Skip to content

Commit

Permalink
Android - implement ChipDnssdStopBrowse in the core SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
pgregorr-amazon committed Apr 1, 2024
1 parent 04251f1 commit 80498b9
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,14 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

public class DiscoveryExampleFragment extends Fragment {
private static final String TAG = DiscoveryExampleFragment.class.getSimpleName();
// 35 represents device type of Matter Casting Player
private static final Long DISCOVERY_TARGET_DEVICE_TYPE = 35L;
private static final int DISCOVERY_RUNTIME_SEC = 15;
private TextView matterDiscoveryMessageTextView;
private static final ScheduledExecutorService executorService =
Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture scheduledFutureTask;
private TextView matterDiscoveryErrorMessageTextView;
private static final List<CastingPlayer> castingPlayerList = new ArrayList<>();
private static ArrayAdapter<CastingPlayer> arrayAdapter;

Expand Down Expand Up @@ -164,15 +159,17 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
matterDiscoveryMessageTextView.setText(
getString(R.string.matter_discovery_message_initializing_text));

matterDiscoveryErrorMessageTextView =
getActivity().findViewById(R.id.matterDiscoveryErrorTextView);
matterDiscoveryErrorMessageTextView.setText(
getString(R.string.matter_discovery_error_message_initial));

arrayAdapter = new CastingPlayerArrayAdapter(getActivity(), castingPlayerList);
final ListView list = getActivity().findViewById(R.id.castingPlayerList);
list.setAdapter(arrayAdapter);

Log.d(TAG, "onViewCreated() creating callbacks");

// TODO: In following PRs. Enable startDiscoveryButton and stopDiscoveryButton when
// stopDiscovery is implemented in the core Matter SDK DNS-SD API. Enable in
// fragment_matter_discovery_example.xml
Button startDiscoveryButton = getView().findViewById(R.id.startDiscoveryButton);
startDiscoveryButton.setOnClickListener(
v -> {
Expand All @@ -188,8 +185,6 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
v -> {
Log.i(TAG, "onViewCreated() stopDiscoveryButton button clicked. Calling stopDiscovery()");
stopDiscovery();
Log.i(TAG, "onViewCreated() stopDiscoveryButton button clicked. Canceling future task");
scheduledFutureTask.cancel(true);
});

Button clearDiscoveryResultsButton = getView().findViewById(R.id.clearDiscoveryResultsButton);
Expand All @@ -198,6 +193,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
Log.i(
TAG, "onViewCreated() clearDiscoveryResultsButton button clicked. Clearing results");
arrayAdapter.clear();
matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_initial));
});
}

Expand All @@ -219,11 +215,7 @@ public void onResume() {
public void onPause() {
super.onPause();
Log.i(TAG, "onPause() called");
// stopDiscovery();
// Don't crash the app
if (scheduledFutureTask != null) {
scheduledFutureTask.cancel(true);
}
stopDiscovery();
}

/** Interface for notifying the host. */
Expand All @@ -235,6 +227,7 @@ public interface Callback {

private boolean startDiscovery() {
Log.i(TAG, "startDiscovery() called");
matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_initial));

arrayAdapter.clear();

Expand All @@ -244,13 +237,15 @@ private boolean startDiscovery() {
matterCastingPlayerDiscovery.addCastingPlayerChangeListener(castingPlayerChangeListener);
if (err.hasError()) {
Log.e(TAG, "startDiscovery() addCastingPlayerChangeListener() called, err Add: " + err);
matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_stop_before_starting) + err);
return false;
}
// Start discovery
Log.i(TAG, "startDiscovery() calling CastingPlayerDiscovery.startDiscovery()");
err = matterCastingPlayerDiscovery.startDiscovery(DISCOVERY_TARGET_DEVICE_TYPE);
if (err.hasError()) {
Log.e(TAG, "startDiscovery() startDiscovery() called, err Start: " + err);
matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_start_error) + err);
return false;
}

Expand All @@ -259,37 +254,21 @@ private boolean startDiscovery() {
matterDiscoveryMessageTextView.setText(
getString(R.string.matter_discovery_message_discovering_text));

// TODO: In following PRs. Enable this to auto-stop discovery after stopDiscovery is
// implemented in the core Matter SDK DNS-SD API.
// Schedule a service to stop discovery and remove the CastingPlayerChangeListener
// Safe to call if discovery is not running
// scheduledFutureTask =
// executorService.schedule(
// () -> {
// Log.i(
// TAG,
// "startDiscovery() executorService "
// + DISCOVERY_RUNTIME_SEC
// + " seconds timer expired. Auto-calling stopDiscovery()");
// stopDiscovery();
// },
// DISCOVERY_RUNTIME_SEC,
// TimeUnit.SECONDS);

return true;
}

private void stopDiscovery() {
Log.i(TAG, "stopDiscovery() called");
matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_initial));

// Stop discovery
MatterError err = matterCastingPlayerDiscovery.stopDiscovery();
if (err.hasError()) {
Log.e(
TAG,
"stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() called, err Stop: " + err);
matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_stop_error) + err);
} else {
// TODO: In following PRs. Implement stop discovery in the Android core API.
Log.d(TAG, "stopDiscovery() MatterCastingPlayerDiscovery.stopDiscovery() success");
}

Expand All @@ -309,6 +288,7 @@ private void stopDiscovery() {
TAG,
"stopDiscovery() matterCastingPlayerDiscovery.removeCastingPlayerChangeListener() called, err Remove: "
+ err);
matterDiscoveryErrorMessageTextView.setText(getString(R.string.matter_discovery_error_message_stop_error) + err);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ JNI_METHOD(jobject, removeCastingPlayerChangeListener)(JNIEnv * env, jobject, jo

return support::convertMatterErrorFromCppToJava(CHIP_NO_ERROR);
}
else if (DiscoveryDelegateImpl::GetInstance()->castingPlayerChangeListenerJavaObject.ObjectRef() == nullptr)
{
ChipLogError(AppServer,
"CastingPlayerDiscovery-JNI::removeCastingPlayerChangeListener() Warning: Cannot remove listener. No "
"listener was added");
return support::convertMatterErrorFromCppToJava(CHIP_NO_ERROR);
}
else
{
ChipLogError(AppServer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -15,14 +15,14 @@
android:padding="10sp">

<Button
android:enabled="false"
android:enabled="true"
android:id="@+id/startDiscoveryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/matter_start_discovery_button_text" />

<Button
android:enabled="false"
android:enabled="true"
android:id="@+id/stopDiscoveryButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -49,5 +49,13 @@
android:fadeScrollbars = "false" />
</LinearLayout>

<TextView
android:id="@+id/matterDiscoveryErrorTextView"
android:layout_width="match_parent"
android:layout_height = "wrap_content"
android:text="string/matter_discovery_error_default_text"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:layout_alignParentBottom="true"/>

</FrameLayout>
</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
<string name="matter_stop_discovery_button_text">Stop Discovery"</string>
<string name="matter_clear_results_button_text">Clear Results"</string>
<string name="matter_discovery_message_initializing_text">Initializing</string>
<string name="matter_discovery_message_discovering_text">Casting Players on-network:</string>
<string name="matter_discovery_message_discovering_text">Discovering Casting Players on-network:</string>
<string name="matter_discovery_message_stopped_text">Discovery Stopped</string>
<string name="matter_discovery_error_message_initial">No errors to report</string>
<string name="matter_discovery_error_message_start_error">START DISCOVERY error. -</string>
<string name="matter_discovery_error_message_stop_before_starting">START DISCOVERY error. Discovery ongoing, STOP before STARTING. -</string>
<string name="matter_discovery_error_message_stop_error">STOP DISCOVERY error. -</string>
<string name="matter_connection_next_button_text">Next</string>
<string name="matter_action_selector_text">Select an action</string>
<string name="matter_select_content_launcher_launch_url_button_text">Content Launcher Launch URL</string>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/dnssd/Discovery_ImplPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ CHIP_ERROR DiscoveryImplPlatform::StopDiscovery(DiscoveryContext & context)
const auto browseIdentifier = context.GetBrowseIdentifier().Value();
context.ClearBrowseIdentifier();

return ChipDnssdStopBrowse(browseIdentifier);
return ChipDnssdStopBrowse(browseIdentifier, HandleNodeBrowse);
}

CHIP_ERROR DiscoveryImplPlatform::ReconfirmRecord(const char * hostname, Inet::IPAddress address, Inet::InterfaceId interfaceId)
Expand Down
4 changes: 3 additions & 1 deletion src/lib/dnssd/platform/Dnssd.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi
*
* @param browseIdentifier an identifier for a browse operation that came from
* ChipDnssdBrowse.
* @param callback The callback to call when the browse is stopped.
*
*/
CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier);
CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier, DnssdBrowseCallback callback = {});

#if CHIP_DEVICE_LAYER_TARGET_DARWIN
/**
Expand Down
22 changes: 20 additions & 2 deletions src/platform/android/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ JniGlobalReference sBrowserObject;
JniGlobalReference sMdnsCallbackObject;
jmethodID sResolveMethod = nullptr;
jmethodID sBrowseMethod = nullptr;
jmethodID sStopBrowseMethod = nullptr;
jmethodID sGetTextEntryKeysMethod = nullptr;
jmethodID sGetTextEntryDataMethod = nullptr;
jclass sMdnsCallbackClass = nullptr;
Expand Down Expand Up @@ -207,9 +208,24 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine
return CHIP_NO_ERROR;
}

CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier, DnssdBrowseCallback callback)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
VerifyOrReturnError(callback != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(sMdnsCallbackObject.HasValidObjectRef(), CHIP_ERROR_INCORRECT_STATE);

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();

env->CallVoidMethod(sBrowserObject.ObjectRef(), sStopBrowseMethod, reinterpret_cast<jlong>(callback), sMdnsCallbackObject.ObjectRef());

if (env->ExceptionCheck())
{
ChipLogError(Discovery, "Java exception in ChipDnssdStopBrowse");
env->ExceptionDescribe();
env->ExceptionClear();
return CHIP_JNI_ERROR_EXCEPTION_THROWN;
}
ChipLogProgress(AppServer, "PHILIPGREGOR DnssdlImpl.cpp ChipDnssdStopBrowse() success, return CHIP_NO_ERROR; 2");
return CHIP_NO_ERROR;
}

template <size_t N>
Expand Down Expand Up @@ -306,6 +322,8 @@ void InitializeWithObjects(jobject resolverObject, jobject browserObject, jobjec

sBrowseMethod = env->GetMethodID(browserClass, "browse", "(Ljava/lang/String;JJLchip/platform/ChipMdnsCallback;)V");

sStopBrowseMethod = env->GetMethodID(browserClass, "stopDiscover", "(JLchip/platform/ChipMdnsCallback;)V");

if (sResolveMethod == nullptr)
{
ChipLogError(Discovery, "Failed to access Resolver 'resolve' method");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Handler;
import android.os.MessageQueue;
import android.os.Looper;
import android.util.Log;
import java.util.ArrayList;
Expand Down Expand Up @@ -97,24 +98,30 @@ public void startDiscover(

public void stopDiscover(final long callbackHandle, final ChipMdnsCallback chipMdnsCallback) {
if (!callbackMap.containsKey(callbackHandle)) {
Log.d(TAG, "Invalid callbackHandle");
return;
}

MessageQueue queue = mainThreadHandler.getLooper().getQueue();
if (!queue.isIdle()) {
Log.i(TAG, "canceling scheduled browse timeout runnable");
mainThreadHandler.removeCallbacksAndMessages(null);
}

NsdManagerDiscovery discovery = callbackMap.remove(callbackHandle);
if (multicastLock.isHeld()) {
multicastLock.release();
}

discovery.setChipMdnsCallback(chipMdnsCallback);
this.nsdManager.stopServiceDiscovery(discovery);
discovery.handleServiceBrowse(chipMdnsCallback);
}

public class NsdManagerDiscovery implements NsdManager.DiscoveryListener {
private String serviceType;
private long callbackHandle;
private long contextHandle;
private ArrayList<String> serviceNameList = new ArrayList<>();
private ChipMdnsCallback chipMdnsCallback;

public NsdManagerDiscovery(String serviceType, long callbackHandle, long contextHandle) {
this.serviceType = serviceType;
Expand Down Expand Up @@ -151,6 +158,7 @@ public void onStopDiscoveryFailed(String serviceType, int errorCode) {
@Override
public void onDiscoveryStopped(String serviceType) {
Log.w(TAG, "Succeed to stop discovery service '" + serviceType);
this.handleServiceBrowse(chipMdnsCallback);
}

public void handleServiceBrowse(ChipMdnsCallback chipMdnsCallback) {
Expand All @@ -160,5 +168,9 @@ public void handleServiceBrowse(ChipMdnsCallback chipMdnsCallback) {
callbackHandle,
contextHandle);
}

public void setChipMdnsCallback (final ChipMdnsCallback chipMdnsCallback) {
this.chipMdnsCallback = chipMdnsCallback;
}
}
}

0 comments on commit 80498b9

Please sign in to comment.