Skip to content

Commit

Permalink
tv-casting-app: simplified android discovery API
Browse files Browse the repository at this point in the history
  • Loading branch information
pgregorr-amazon committed Dec 14, 2023
1 parent 97cabdf commit 53d79a5
Show file tree
Hide file tree
Showing 25 changed files with 1,029 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.chip.casting.MatterError;
import com.chip.casting.SuccessCallback;
import com.chip.casting.TvCastingApp;
import com.chip.casting.util.GlobalCastingConstants;
import com.matter.casting.DiscoveryExample;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -39,6 +41,9 @@ public class CommissionerDiscoveryFragment extends Fragment {
private ScheduledExecutorService executor;

public CommissionerDiscoveryFragment(TvCastingApp tvCastingApp) {
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment() called, constructor, tvCastingApp: "
+ tvCastingApp);
this.tvCastingApp = tvCastingApp;
this.executor = Executors.newSingleThreadScheduledExecutor();
}
Expand All @@ -50,6 +55,9 @@ public CommissionerDiscoveryFragment(TvCastingApp tvCastingApp) {
* @return A new instance of fragment CommissionerDiscoveryFragment.
*/
public static CommissionerDiscoveryFragment newInstance(TvCastingApp tvCastingApp) {
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment newInstance() called with tvCastingApp: "
+ tvCastingApp);
return new CommissionerDiscoveryFragment(tvCastingApp);
}

Expand All @@ -67,6 +75,8 @@ public View onCreateView(

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onViewCreated() called");
super.onViewCreated(view, savedInstanceState);

Button manualCommissioningButton = getView().findViewById(R.id.manualCommissioningButton);
Expand Down Expand Up @@ -99,10 +109,15 @@ public void onClick(View v) {
final ListView list = getActivity().findViewById(R.id.commissionerList);
list.setAdapter(arrayAdapter);

System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onViewCreated(), creating Callbacks");
this.successCallback =
new SuccessCallback<DiscoveredNodeData>() {
@Override
public void handle(DiscoveredNodeData discoveredNodeData) {
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onViewCreated() successCallback called, discoveredNodeData: "
+ discoveredNodeData);
Log.d(TAG, "Discovered a Video Player Commissioner: " + discoveredNodeData);
new Handler(Looper.getMainLooper())
.post(
Expand All @@ -129,6 +144,8 @@ public void handle(DiscoveredNodeData discoveredNodeData) {
new FailureCallback() {
@Override
public void handle(MatterError matterError) {
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onViewCreated() failureCallback called");
Log.e(TAG, "Error occurred during video player commissioner discovery: " + matterError);
if (MatterError.DISCOVERY_SERVICE_LOST == matterError) {
Log.d(TAG, "Attempting to restart service");
Expand All @@ -148,21 +165,44 @@ public void handle(MatterError matterError) {
@Override
public void onResume() {
super.onResume();
System.out.println("PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onResume() called");
Log.d(TAG, "Auto discovering");

poller =
executor.scheduleAtFixedRate(
() -> {
tvCastingApp.discoverVideoPlayerCommissioners(successCallback, failureCallback);
},
0,
DISCOVERY_POLL_INTERVAL_MS,
TimeUnit.MILLISECONDS);
if (GlobalCastingConstants.ChipCastingSimplified) {
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onResume() Auto discovering... - SimplifiedAPI");
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onResume() tvCastingApp: "
+ tvCastingApp);
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onResume() calling DiscoveryExample.demoDiscovery()");
DiscoveryExample.demoDiscovery(successCallback, failureCallback);
} else {
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onResume() Auto discovering... - Old API");
poller =
executor.scheduleAtFixedRate(
() -> {
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onResume() calling TvCastingApp.discoverVideoPlayerCommissioners() with successCallback and failureCallback");
System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onResume() tvCastingApp: "
+ tvCastingApp);
tvCastingApp.discoverVideoPlayerCommissioners(successCallback, failureCallback);
},
0,
DISCOVERY_POLL_INTERVAL_MS, // 15,000 ms
TimeUnit.MILLISECONDS);

System.out.println(
"PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onResume() called, Auto discovering, pooling...");
}
}

@Override
public void onPause() {
super.onPause();
System.out.println("PHILIPGREGOR java-chip \tCommissionerDiscoveryFragment onPause() called");
tvCastingApp.stopVideoPlayerDiscovery();
poller.cancel(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public class MainActivity extends AppCompatActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("PHILIPGREGOR java-chip \tMainActivity onCreate() called");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

System.out.println(
"PHILIPGREGOR java-chip \tMainActivity onCreate() calling InitializationExample.initAndStart() or MainActivity.initJni()");
boolean ret =
GlobalCastingConstants.ChipCastingSimplified
? InitializationExample.initAndStart(this.getApplicationContext()).hasNoError()
Expand All @@ -35,6 +37,7 @@ protected void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "Failed to initialize Matter TV casting library");
return;
}
System.out.println("PHILIPGREGOR java-chip \tMainActivity onCreate() ret: " + ret);

Fragment fragment = CommissionerDiscoveryFragment.newInstance(tvCastingApp);
getSupportFragmentManager()
Expand All @@ -45,31 +48,37 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public void handleCommissioningButtonClicked(DiscoveredNodeData commissioner) {
System.out.println("PHILIPGREGOR java-chip \tMainActivity handleCommissioningButtonClicked()");
showFragment(ConnectionFragment.newInstance(tvCastingApp, commissioner));
}

@Override
public void handleCommissioningComplete() {
System.out.println("PHILIPGREGOR java-chip \tMainActivity handleCommissioningComplete()");
showFragment(SelectClusterFragment.newInstance(tvCastingApp));
}

@Override
public void handleContentLauncherSelected() {
System.out.println("PHILIPGREGOR java-chip \tMainActivity handleContentLauncherSelected()");
showFragment(ContentLauncherFragment.newInstance(tvCastingApp));
}

@Override
public void handleCertTestLauncherSelected() {
System.out.println("PHILIPGREGOR java-chip \tMainActivity handleCertTestLauncherSelected()");
showFragment(CertTestFragment.newInstance(tvCastingApp));
}

@Override
public void handleMediaPlaybackSelected() {
System.out.println("PHILIPGREGOR java-chip \tMainActivity handleMediaPlaybackSelected()");
showFragment(MediaPlaybackFragment.newInstance(tvCastingApp));
}

@Override
public void handleDisconnect() {
System.out.println("PHILIPGREGOR java-chip \tMainActivity handleDisconnect()");
showFragment(CommissionerDiscoveryFragment.newInstance(tvCastingApp));
}

Expand All @@ -79,6 +88,10 @@ public void handleDisconnect() {
* TvCastingApp
*/
private boolean initJni() {
System.out.println("PHILIPGREGOR java-chip \tMainActivity initJni() called - CHIP");

System.out.println(
"PHILIPGREGOR java-chip \tMainActivity initJni() calling TvCastingApp.getInstance()");
tvCastingApp = TvCastingApp.getInstance();

tvCastingApp.setDACProvider(new com.chip.casting.util.DACProviderStub());
Expand All @@ -97,6 +110,7 @@ private boolean initJni() {
}

private void showFragment(Fragment fragment, boolean showOnBack) {
System.out.println("PHILIPGREGOR java-chip \tMainActivity showFragment() called");
Log.d(
TAG,
"showFragment called with " + fragment.getClass().getSimpleName() + " and " + showOnBack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public class GlobalCastingConstants {
public static final int SetupPasscode = 20202021;
public static final int Discriminator = 0xF00;
public static final boolean ChipCastingSimplified =
false; // set this flag to true to demo simplified casting APIs
true; // set this flag to true to demo simplified casting APIs
// PHILIPGREGOR, change to false before commit
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,21 @@ public class PreferencesConfigurationManager implements ConfigurationManager {
private SharedPreferences preferences;

public PreferencesConfigurationManager(Context context, String preferenceFileKey) {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager() preferenceFileKey: "
+ preferenceFileKey);
preferences = context.getSharedPreferences(preferenceFileKey, Context.MODE_PRIVATE);

Map<String, String> appMetadata = (Map<String, String>) preferences.getAll();

for (Map.Entry<String, String> entry : appMetadata.entrySet()) {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager() Key: "
+ entry.getKey()
+ " Value: "
+ entry.getValue());
}

try {
String keyUniqueId = getKey(kConfigNamespace_ChipFactory, kConfigKey_UniqueId);
if (!preferences.contains(keyUniqueId)) {
Expand All @@ -52,32 +65,58 @@ public PreferencesConfigurationManager(Context context, String preferenceFileKey
@Override
public long readConfigValueLong(String namespace, String name)
throws AndroidChipPlatformException {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager readConfigValueLong() namespace: "
+ namespace
+ " name: "
+ name);
String key = getKey(namespace, name);
if (preferences.contains(key)) {
long value = preferences.getLong(key, Long.MAX_VALUE);
return value;
} else {
Log.d(TAG, "Key '" + key + "' not found in shared preferences");
throw new AndroidChipPlatformException();
// throw new AndroidChipPlatformException();
return 0;
}
}

@Override
public String readConfigValueStr(String namespace, String name)
throws AndroidChipPlatformException {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager readConfigValueStr() namespace: "
+ namespace
+ " name: "
+ name);
String key = getKey(namespace, name);
if (preferences.contains(key)) {
String value = preferences.getString(key, null);
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager readConfigValueStr() key: "
+ key);
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager readConfigValueStr() preferences: "
+ preferences.getAll().toString());
if (preferences.contains(name)) {
String value = preferences.getString(name, null);
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager readConfigValueStr() value: "
+ value);
return value;
} else {
Log.d(TAG, "Key '" + key + "' not found in shared preferences");
throw new AndroidChipPlatformException();
// throw new AndroidChipPlatformException();
return null;
}
}

@Override
public byte[] readConfigValueBin(String namespace, String name)
throws AndroidChipPlatformException {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager readConfigValueBin() namespace: "
+ namespace
+ " name: "
+ name);
String key = getKey(namespace, name);
if (preferences.contains(key)) {
String value = preferences.getString(key, null);
Expand All @@ -92,20 +131,35 @@ public byte[] readConfigValueBin(String namespace, String name)
@Override
public void writeConfigValueLong(String namespace, String name, long val)
throws AndroidChipPlatformException {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager writeConfigValueLong() namespace: "
+ namespace
+ " name: "
+ name);
String key = getKey(namespace, name);
preferences.edit().putLong(key, val).apply();
}

@Override
public void writeConfigValueStr(String namespace, String name, String val)
throws AndroidChipPlatformException {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager writeConfigValueStr() namespace: "
+ namespace
+ " name: "
+ name);
String key = getKey(namespace, name);
preferences.edit().putString(key, val).apply();
}

@Override
public void writeConfigValueBin(String namespace, String name, byte[] val)
throws AndroidChipPlatformException {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager writeConfigValueBin() namespace: "
+ namespace
+ " name: "
+ name);
String key = getKey(namespace, name);
if (val != null) {
String valStr = Base64.getEncoder().encodeToString(val);
Expand All @@ -117,6 +171,11 @@ public void writeConfigValueBin(String namespace, String name, byte[] val)

@Override
public void clearConfigValue(String namespace, String name) throws AndroidChipPlatformException {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager clearConfigValue() namespace: "
+ namespace
+ " name: "
+ name);
if (namespace != null && name != null) {
preferences.edit().remove(getKey(namespace, name)).apply();
} else if (namespace != null && name == null) {
Expand All @@ -138,6 +197,11 @@ public void clearConfigValue(String namespace, String name) throws AndroidChipPl
@Override
public boolean configValueExists(String namespace, String name)
throws AndroidChipPlatformException {
System.out.println(
"PHILIPGREGOR java-chip \tPreferencesConfigurationManager configValueExists() namespace: "
+ namespace
+ " name: "
+ name);
return preferences.contains(getKey(namespace, name));
}

Expand Down
Loading

0 comments on commit 53d79a5

Please sign in to comment.