Skip to content

Commit

Permalink
Refactor/clean-up android tv-casting-app (#18701)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon authored and pull[bot] committed Jul 6, 2023
1 parent a170ae1 commit 4483724
Show file tree
Hide file tree
Showing 26 changed files with 369 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.MatterCallbackHandler;
import com.chip.casting.TvCastingApp;
import com.chip.casting.dnssd.DiscoveredNodeData;
import com.chip.casting.platform.MatterCallbackHandler;
import com.chip.casting.util.GlobalCastingConstants;

/** A {@link Fragment} to get the TV Casting App commissioned. */
Expand All @@ -20,7 +20,6 @@ public class CommissioningFragment extends Fragment {
private final TvCastingApp tvCastingApp;
private final DiscoveredNodeData selectedCommissioner;

private boolean initServerSuccess;
private boolean openCommissioningWindowSuccess;
private boolean sendUdcSuccess;

Expand Down Expand Up @@ -49,58 +48,54 @@ public void onCreate(Bundle savedInstanceState) {
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Callback callback = (CommissioningFragment.Callback) this.getActivity();
this.initServerSuccess =
tvCastingApp.initServer(
this.openCommissioningWindowSuccess =
tvCastingApp.openBasicCommissioningWindow(
GlobalCastingConstants.CommissioningWindowDurationSecs,
new MatterCallbackHandler() {
@Override
public boolean handle(boolean success) {
Log.d(
TAG, "handle() called on CommissioningComplete event with success " + success);
if (success) {
public void handle(Status status) {
Log.d(TAG, "handle() called on CommissioningComplete event with " + status);
if (status.isSuccess()) {
callback.handleCommissioningComplete();
}
return true;
}
});
if (this.openCommissioningWindowSuccess) {
if (selectedCommissioner != null && selectedCommissioner.getNumIPs() > 0) {
String ipAddress = selectedCommissioner.getIpAddresses().get(0).getHostAddress();
Log.d(
TAG,
"CommissioningFragment calling tvCastingApp.sendUserDirectedCommissioningRequest with IP: "
+ ipAddress
+ " port: "
+ selectedCommissioner.getPort());

if (this.initServerSuccess) {
this.openCommissioningWindowSuccess =
tvCastingApp.openBasicCommissioningWindow(
GlobalCastingConstants.CommissioningWindowDurationSecs);
if (this.openCommissioningWindowSuccess) {
if (selectedCommissioner != null && selectedCommissioner.getNumIPs() > 0) {
String ipAddress = selectedCommissioner.getIpAddresses().get(0).getHostAddress();
Log.d(
TAG,
"CommissioningFragment calling tvCastingApp.sendUserDirectedCommissioningRequest with IP: "
+ ipAddress
+ " port: "
+ selectedCommissioner.getPort());

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

return inflater.inflate(R.layout.fragment_commissioning, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String commissioningWindowStatus = "Failed to open commissioning window";
if (this.initServerSuccess) {
if (this.openCommissioningWindowSuccess) {
commissioningWindowStatus = "Commissioning window has been opened. Commission manually.";
if (this.sendUdcSuccess) {
commissioningWindowStatus =
"Commissioning window has been opened. Commissioning requested from "
+ selectedCommissioner.getDeviceName();
}
TextView onboardingPayloadView = getView().findViewById(R.id.onboardingPayload);
onboardingPayloadView.setText("Onboarding PIN: " + GlobalCastingConstants.SetupPasscode);
if (this.openCommissioningWindowSuccess) {
commissioningWindowStatus = "Commissioning window has been opened. Commission manually.";
if (this.sendUdcSuccess) {
commissioningWindowStatus =
"Commissioning window has been opened. Commissioning requested from "
+ selectedCommissioner.getDeviceName();
}
TextView onboardingPayloadView = getView().findViewById(R.id.onboardingPayload);
onboardingPayloadView.setText(
"Onboarding PIN: "
+ GlobalCastingConstants.SetupPasscode
+ "\nDiscriminator: "
+ GlobalCastingConstants.Discriminator);
}

TextView commissioningWindowStatusView = getView().findViewById(R.id.commissioningWindowStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.chip.casting.MatterCallbackHandler;
import com.chip.casting.TvCastingApp;

/** A {@link Fragment} to send Content Launcher commands from the TV Casting App. */
Expand Down Expand Up @@ -49,7 +51,16 @@ public void onClick(View v) {
EditText contentDisplayString =
getView().findViewById(R.id.contentDisplayStringEditText);
tvCastingApp.contentLauncherLaunchURL(
contentUrl.getText().toString(), contentDisplayString.getText().toString());
contentUrl.getText().toString(),
contentDisplayString.getText().toString(),
new MatterCallbackHandler() {
@Override
public void handle(Status status) {
Log.d(TAG, "handle() called on LaunchURLResponse with success " + status);
TextView launchUrlStatus = getView().findViewById(R.id.launchUrlStatus);
launchUrlStatus.setText(status.isSuccess() ? "Success!" : "Failure!");
}
});
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
Expand All @@ -21,6 +22,8 @@
public class MainActivity extends AppCompatActivity
implements CommissionerDiscoveryFragment.Callback, CommissioningFragment.Callback {

private static final String TAG = MainActivity.class.getSimpleName();

private ChipAppServer chipAppServer;
private TvCastingApp tvCastingApp;

Expand Down Expand Up @@ -48,9 +51,13 @@ public void handleCommissioningComplete() {
showFragment(ContentLauncherFragment.newInstance(tvCastingApp));
}

/**
* The order is important, must first new TvCastingApp to load dynamic library, then
* AndroidChipPlatform to prepare platform, then start ChipAppServer, then call init on
* TvCastingApp
*/
private void initJni() {
tvCastingApp =
new TvCastingApp((app, clusterId, duration) -> app.openBasicCommissioningWindow(duration));
tvCastingApp = new TvCastingApp();

tvCastingApp.setDACProvider(new DACProviderStub());
Context applicationContext = this.getApplicationContext();
Expand All @@ -68,10 +75,13 @@ private void initJni() {

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

tvCastingApp.init();
}

private void showFragment(Fragment fragment, boolean showOnBack) {
System.out.println(
Log.d(
TAG,
"showFragment called with " + fragment.getClass().getSimpleName() + " and " + showOnBack);
FragmentTransaction fragmentTransaction =
getSupportFragmentManager()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import androidx.annotation.VisibleForTesting;
import com.chip.casting.app.CastingContext;
import com.chip.casting.app.CommissionerDiscoveryFragment;
import com.chip.casting.util.GlobalCastingConstants;
import java.util.List;

public class CommissionerResolveListener implements NsdManager.ResolveListener {
Expand All @@ -27,28 +28,30 @@ public CommissionerResolveListener(
@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
DiscoveredNodeData commissioner = new DiscoveredNodeData(serviceInfo);
commissioners.add(commissioner);
Log.d(TAG, "Commissioner resolved: " + commissioner);

String buttonText = getCommissionerButtonText(commissioner);
if (!buttonText.isEmpty()) {
Button commissionerButton = new Button(castingContext.getApplicationContext());
commissionerButton.setText(buttonText);
CommissionerDiscoveryFragment.Callback callback =
(CommissionerDiscoveryFragment.Callback) castingContext.getFragmentActivity();
commissionerButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(
TAG,
"CommissionerResolveListener.onServiceResolved.OnClickListener.onClick called for "
+ commissioner);
callback.handleCommissioningButtonClicked(commissioner);
}
});
new Handler(Looper.getMainLooper())
.post(() -> castingContext.getCommissionersLayout().addView(commissionerButton));
if (isPassingDeviceTypeFilter(commissioner)) {
commissioners.add(commissioner);
String buttonText = getCommissionerButtonText(commissioner);
if (!buttonText.isEmpty()) {
Button commissionerButton = new Button(castingContext.getApplicationContext());
commissionerButton.setText(buttonText);
CommissionerDiscoveryFragment.Callback callback =
(CommissionerDiscoveryFragment.Callback) castingContext.getFragmentActivity();
commissionerButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(
TAG,
"CommissionerResolveListener.onServiceResolved.OnClickListener.onClick called for "
+ commissioner);
callback.handleCommissioningButtonClicked(commissioner);
}
});
new Handler(Looper.getMainLooper())
.post(() -> castingContext.getCommissionersLayout().addView(commissionerButton));
}
} else Log.e(TAG, "Skipped displaying " + commissioner);
}

Expand Down Expand Up @@ -87,4 +90,11 @@ public String getCommissionerButtonText(DiscoveredNodeData commissioner) {
aux = aux.isEmpty() ? aux : "\n[" + aux + "]";
return main + aux;
}

private boolean isPassingDeviceTypeFilter(DiscoveredNodeData commissioner) {
return GlobalCastingConstants.CommissionerDeviceTypeFilter == null
|| GlobalCastingConstants.CommissionerDeviceTypeFilter.isEmpty()
|| GlobalCastingConstants.CommissionerDeviceTypeFilter.contains(
commissioner.getDeviceType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,122 +56,62 @@ public String getHostName() {
return hostName;
}

public void setHostName(String hostName) {
this.hostName = hostName;
}

public String getInstanceName() {
return instanceName;
}

public void setInstanceName(String instanceName) {
this.instanceName = instanceName;
}

public long getLongDiscriminator() {
return longDiscriminator;
}

public void setLongDiscriminator(long longDiscriminator) {
this.longDiscriminator = longDiscriminator;
}

public long getVendorId() {
return vendorId;
}

public void setVendorId(long vendorId) {
this.vendorId = vendorId;
}

public long getProductId() {
return productId;
}

public void setProductId(long productId) {
this.productId = productId;
}

public byte getCommissioningMode() {
return commissioningMode;
}

public void setCommissioningMode(byte commissioningMode) {
this.commissioningMode = commissioningMode;
}

public long getDeviceType() {
return deviceType;
}

public void setDeviceType(long deviceType) {
this.deviceType = deviceType;
}

public String getDeviceName() {
return deviceName;
}

public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}

public byte[] getRotatingId() {
return rotatingId;
}

public void setRotatingId(byte[] rotatingId) {
this.rotatingId = rotatingId;
}

public int getRotatingIdLen() {
return rotatingIdLen;
}

public void setRotatingIdLen(int rotatingIdLen) {
this.rotatingIdLen = rotatingIdLen;
}

public short getPairingHint() {
return pairingHint;
}

public void setPairingHint(short pairingHint) {
this.pairingHint = pairingHint;
}

public String getPairingInstruction() {
return pairingInstruction;
}

public void setPairingInstruction(String pairingInstruction) {
this.pairingInstruction = pairingInstruction;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public int getNumIPs() {
return numIPs;
}

public void setNumIPs(int numIPs) {
this.numIPs = numIPs;
}

public List<InetAddress> getIpAddresses() {
return ipAddresses;
}

public void setIpAddresses(List<InetAddress> ipAddresses) {
this.ipAddresses = ipAddresses;
}

@Override
public String toString() {
return "DiscoveredNodeData{"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.chip.casting.util;

import java.util.Arrays;
import java.util.List;

public class GlobalCastingConstants {
public static final String CommissionerServiceType = "_matterd._udp.";
public static final int CommissioningWindowDurationSecs = 3 * 60;
public static int SetupPasscode = 20202021;
public static int Discriminator = 0xF00;
public static List<Long> CommissionerDeviceTypeFilter = Arrays.asList(35L); // Video player = 35
}
Loading

0 comments on commit 4483724

Please sign in to comment.