Skip to content

Commit

Permalink
Linux/iOS/Android tv-casting-apps: Cache/recall/re-target previously …
Browse files Browse the repository at this point in the history
…connected VideoPlayers/Endpoints (#23181)

* Linux/tv-casting-app: Cache/recall/re-target previously connected VideoPlayers/Endpoints

* iOS/TvCasting app: Cache/recall/re-target previously connected VideoPlayers/Endpoints

* Build fixes

* Android TvCasting app: Cache/recall/re-target previously connected VideoPlayers/Endpoints
  • Loading branch information
sharadb-amazon authored and pull[bot] committed Oct 27, 2023
1 parent f62c669 commit 231ba7a
Show file tree
Hide file tree
Showing 61 changed files with 4,184 additions and 1,072 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.chip.casting.app;

import android.content.Context;
import android.net.nsd.NsdManager;
import android.widget.LinearLayout;
import androidx.fragment.app.FragmentActivity;

Expand All @@ -20,11 +19,6 @@ public FragmentActivity getFragmentActivity() {
return fragmentActivity;
}

public NsdManager getNsdManager() {
return (NsdManager)
fragmentActivity.getApplicationContext().getSystemService(Context.NSD_SERVICE);
}

public LinearLayout getCommissionersLayout() {
return (LinearLayout) fragmentActivity.findViewById(R.id.castingCommissioners);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,46 @@
import android.net.nsd.NsdManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import com.chip.casting.dnssd.CommissionerDiscoveryListener;
import com.chip.casting.dnssd.DiscoveredNodeData;
import com.chip.casting.util.GlobalCastingConstants;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import com.chip.casting.DiscoveredNodeData;
import com.chip.casting.FailureCallback;
import com.chip.casting.MatterError;
import com.chip.casting.SuccessCallback;
import com.chip.casting.TvCastingApp;

/** A {@link Fragment} to discover commissioners on the network */
public class CommissionerDiscoveryFragment extends Fragment {
private static final String TAG = CommissionerDiscoveryFragment.class.getSimpleName();
private static final long DISCOVERY_DURATION_SECS = 10;
private final TvCastingApp tvCastingApp;

public CommissionerDiscoveryFragment() {}
public CommissionerDiscoveryFragment(TvCastingApp tvCastingApp) {
this.tvCastingApp = tvCastingApp;
}

/**
* Use this factory method to create a new instance of this fragment using the provided
* parameters.
*
* @return A new instance of fragment CommissionerDiscoveryFragment.
*/
public static CommissionerDiscoveryFragment newInstance() {
return new CommissionerDiscoveryFragment();
public static CommissionerDiscoveryFragment newInstance(TvCastingApp tvCastingApp) {
return new CommissionerDiscoveryFragment(tvCastingApp);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startCommissionerDiscovery();
}

@Override
Expand All @@ -49,47 +56,84 @@ public View onCreateView(
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

Button manualCommissioningButton = getView().findViewById(R.id.manualCommissioningButton);
Callback callback = (Callback) this.getActivity();
manualCommissioningButton.setOnClickListener(
View.OnClickListener manualCommissioningButtonOnClickListener =
new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.handleCommissioningButtonClicked(null);
}
});
}
};
manualCommissioningButton.setOnClickListener(manualCommissioningButtonOnClickListener);

Context context = this.getContext();
Context applicationContext = this.getContext().getApplicationContext();

SuccessCallback<DiscoveredNodeData> successCallback =
new SuccessCallback<DiscoveredNodeData>() {
@Override
public void handle(DiscoveredNodeData discoveredNodeData) {
Log.d(TAG, "Discovered a Video Player Commissioner: " + discoveredNodeData);
String buttonText = getCommissionerButtonText(discoveredNodeData);

private void startCommissionerDiscovery() {
Log.d(TAG, "CommissionerDiscoveryFragment.startCommissionerDiscovery called");
WifiManager wifi = (WifiManager) this.getContext().getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
if (!buttonText.isEmpty()) {
Button commissionerButton = new Button(context);
commissionerButton.setText(buttonText);
CommissionerDiscoveryFragment.Callback callback =
(CommissionerDiscoveryFragment.Callback) getActivity();
commissionerButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(
TAG,
"CommissionerResolveListener.onServiceResolved.OnClickListener.onClick called for "
+ discoveredNodeData);
callback.handleCommissioningButtonClicked(discoveredNodeData);
}
});
new Handler(Looper.getMainLooper())
.post(
() ->
((LinearLayout) getActivity().findViewById(R.id.castingCommissioners))
.addView(commissionerButton));
}
}
};

CastingContext castingContext = new CastingContext(this.getActivity());
NsdManager.DiscoveryListener commissionerDiscoveryListener =
new CommissionerDiscoveryListener(castingContext);
FailureCallback failureCallback =
new FailureCallback() {
@Override
public void handle(MatterError matterError) {
Log.e(TAG, "Error occurred during video player commissioner discovery: " + matterError);
}
};

NsdManager nsdManager = castingContext.getNsdManager();
nsdManager.discoverServices(
GlobalCastingConstants.CommissionerServiceType,
NsdManager.PROTOCOL_DNS_SD,
commissionerDiscoveryListener);
tvCastingApp.discoverVideoPlayerCommissioners(
(WifiManager) context.getSystemService(Context.WIFI_SERVICE),
(NsdManager) applicationContext.getSystemService(Context.NSD_SERVICE),
DISCOVERY_DURATION_SECS,
successCallback,
failureCallback);
}

// Stop discovery after specified timeout
Executors.newSingleThreadScheduledExecutor()
.schedule(
new Runnable() {
@Override
public void run() {
nsdManager.stopServiceDiscovery(commissionerDiscoveryListener);
multicastLock.release();
}
},
10,
TimeUnit.SECONDS);
Log.d(TAG, "CommissionerDiscoveryFragment.startCommissionerDiscovery ended");
@VisibleForTesting
public String getCommissionerButtonText(DiscoveredNodeData commissioner) {
String main = commissioner.getDeviceName() != null ? commissioner.getDeviceName() : "";
String aux =
"" + (commissioner.getProductId() > 0 ? "Product ID: " + commissioner.getProductId() : "");
aux +=
commissioner.getDeviceType() > 0
? (aux.isEmpty() ? "" : " ") + "Device Type: " + commissioner.getDeviceType()
: "";
aux +=
commissioner.getVendorId() > 0
? (aux.isEmpty() ? "" : " from ") + "Vendor ID: " + commissioner.getVendorId()
: "";
aux = aux.isEmpty() ? aux : "\n[" + aux + "]";
return main + aux;
}

/** Interface for notifying the host. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.ContentApp;
import com.chip.casting.DiscoveredNodeData;
import com.chip.casting.FailureCallback;
import com.chip.casting.MatterCallbackHandler;
import com.chip.casting.MatterError;
import com.chip.casting.SuccessCallback;
import com.chip.casting.TvCastingApp;
import com.chip.casting.dnssd.DiscoveredNodeData;
import com.chip.casting.VideoPlayer;
import com.chip.casting.util.GlobalCastingConstants;

/** A {@link Fragment} to get the TV Casting App commissioned. */
Expand Down Expand Up @@ -56,9 +60,25 @@ public View onCreateView(
@Override
public void handle(MatterError error) {
Log.d(TAG, "handle() called on CommissioningComplete event with " + error);
if (error.isNoError()) {
callback.handleCommissioningComplete();
}
}
},
new SuccessCallback<VideoPlayer>() {
@Override
public void handle(VideoPlayer videoPlayer) {
Log.d(TAG, "handle() called on OnConnectionSuccess with " + videoPlayer);
callback.handleCommissioningComplete();
}
},
new FailureCallback() {
@Override
public void handle(MatterError matterError) {
Log.d(TAG, "handle() called on OnConnectionFailure with " + matterError);
}
},
new SuccessCallback<ContentApp>() {
@Override
public void handle(ContentApp contentApp) {
Log.d(TAG, "handle() called on OnNewOrUpdatedEndpoint with " + contentApp);
}
});
if (this.openCommissioningWindowSuccess) {
Expand All @@ -71,9 +91,7 @@ public void handle(MatterError error) {
+ " port: "
+ selectedCommissioner.getPort());

this.sendUdcSuccess =
tvCastingApp.sendUserDirectedCommissioningRequest(
ipAddress, selectedCommissioner.getPort());
this.sendUdcSuccess = tvCastingApp.sendCommissioningRequest(selectedCommissioner);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.ContentApp;
import com.chip.casting.MatterCallbackHandler;
import com.chip.casting.MatterError;
import com.chip.casting.TvCastingApp;
Expand All @@ -21,6 +22,8 @@ public class ContentLauncherFragment extends Fragment {

private View.OnClickListener launchUrlButtonClickListener;

private static final ContentApp kContentApp = new ContentApp((short) 4, null);

public ContentLauncherFragment(TvCastingApp tvCastingApp) {
this.tvCastingApp = tvCastingApp;
}
Expand Down Expand Up @@ -52,6 +55,7 @@ public void onClick(View v) {
EditText contentDisplayString =
getView().findViewById(R.id.contentDisplayStringEditText);
tvCastingApp.contentLauncherLaunchURL(
kContentApp,
contentUrl.getText().toString(),
contentDisplayString.getText().toString(),
new MatterCallbackHandler() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import chip.platform.PreferencesConfigurationManager;
import chip.platform.PreferencesKeyValueStoreManager;
import com.chip.casting.DACProviderStub;
import com.chip.casting.DiscoveredNodeData;
import com.chip.casting.TvCastingApp;
import com.chip.casting.dnssd.DiscoveredNodeData;
import com.chip.casting.util.GlobalCastingConstants;

public class MainActivity extends AppCompatActivity
Expand All @@ -37,7 +37,7 @@ protected void onCreate(Bundle savedInstanceState) {

initJni();

Fragment fragment = CommissionerDiscoveryFragment.newInstance();
Fragment fragment = CommissionerDiscoveryFragment.newInstance(tvCastingApp);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.main_fragment_container, fragment, fragment.getClass().getSimpleName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.ContentApp;
import com.chip.casting.FailureCallback;
import com.chip.casting.MatterError;
import com.chip.casting.MediaPlaybackTypes;
Expand All @@ -23,6 +24,8 @@ public class MediaPlaybackFragment extends Fragment {

private View.OnClickListener subscribeToCurrentStateButtonClickListener;

private static final ContentApp kContentApp = new ContentApp((short) 4, null);

public MediaPlaybackFragment(TvCastingApp tvCastingApp) {
this.tvCastingApp = tvCastingApp;
}
Expand Down Expand Up @@ -107,6 +110,7 @@ public void run() {

boolean retVal =
tvCastingApp.mediaPlayback_subscribeToCurrentState(
kContentApp,
successCallback,
failureCallback,
Integer.parseInt(minInterval.getText().toString()),
Expand Down

This file was deleted.

Loading

0 comments on commit 231ba7a

Please sign in to comment.