Skip to content

Commit 389a76d

Browse files
Restyled
1 parent 01a26df commit 389a76d

File tree

3 files changed

+157
-158
lines changed

3 files changed

+157
-158
lines changed

examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ApplicationBasicReadVendorIDExampleFragment.java

+29-34
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,20 @@
2323
import android.view.LayoutInflater;
2424
import android.view.View;
2525
import android.view.ViewGroup;
26-
import android.widget.EditText;
2726
import android.widget.TextView;
28-
2927
import androidx.annotation.Nullable;
3028
import androidx.fragment.app.Fragment;
31-
29+
import chip.devicecontroller.ChipClusters;
3230
import com.R;
3331
import com.matter.casting.core.CastingPlayer;
3432
import com.matter.casting.core.Endpoint;
3533

36-
import java.util.List;
37-
import java.util.Optional;
38-
39-
import chip.devicecontroller.ChipClusters;
40-
41-
/** A {@link Fragment} to read the VendorID (from ApplicationBasic cluster) using the TV Casting App. */
34+
/**
35+
* A {@link Fragment} to read the VendorID (from ApplicationBasic cluster) using the TV Casting App.
36+
*/
4237
public class ApplicationBasicReadVendorIDExampleFragment extends Fragment {
43-
private static final String TAG = ApplicationBasicReadVendorIDExampleFragment.class.getSimpleName();
38+
private static final String TAG =
39+
ApplicationBasicReadVendorIDExampleFragment.class.getSimpleName();
4440

4541
private final CastingPlayer selectedCastingPlayer;
4642

@@ -74,9 +70,7 @@ public View onCreateView(
7470
v -> {
7571
Endpoint endpoint = EndpointSelectorExample.selectEndpointByVID(selectedCastingPlayer);
7672
if (endpoint == null) {
77-
Log.e(
78-
TAG,
79-
"No Endpoint with sample vendorID found on CastingPlayer");
73+
Log.e(TAG, "No Endpoint with sample vendorID found on CastingPlayer");
8074
return;
8175
}
8276

@@ -92,32 +86,33 @@ public View onCreateView(
9286

9387
// call readVendorIDAttribute on the cluster object while passing in a
9488
// ChipClusters.IntegerAttributeCallback
95-
cluster.readVendorIDAttribute(new ChipClusters.IntegerAttributeCallback() {
96-
@Override
97-
public void onSuccess(int value) {
89+
cluster.readVendorIDAttribute(
90+
new ChipClusters.IntegerAttributeCallback() {
91+
@Override
92+
public void onSuccess(int value) {
9893
Log.d(TAG, "ReadVendorID success. Value: " + value);
9994
new Handler(Looper.getMainLooper())
100-
.post(
101-
() -> {
102-
TextView vendorIdResult = getView().findViewById(R.id.vendorIdResult);
103-
vendorIdResult.setText(
104-
"Read VendorID result\nValue: " + value );
105-
});
106-
}
107-
108-
@Override
109-
public void onError(Exception error) {
95+
.post(
96+
() -> {
97+
TextView vendorIdResult = getView().findViewById(R.id.vendorIdResult);
98+
vendorIdResult.setText("Read VendorID result\nValue: " + value);
99+
});
100+
}
101+
102+
@Override
103+
public void onError(Exception error) {
110104
Log.e(TAG, "ReadVendorID failure " + error);
111105
new Handler(Looper.getMainLooper())
112-
.post(
113-
() -> {
114-
TextView vendorIdResult = getView().findViewById(R.id.vendorIdResult);
115-
vendorIdResult.setText("Read VendorID result\nError: " + error);
116-
});
117-
}
118-
});
106+
.post(
107+
() -> {
108+
TextView vendorIdResult = getView().findViewById(R.id.vendorIdResult);
109+
vendorIdResult.setText("Read VendorID result\nError: " + error);
110+
});
111+
}
112+
});
119113
};
120-
return inflater.inflate(R.layout.fragment_matter_application_basic_read_vendor_id, container, false);
114+
return inflater.inflate(
115+
R.layout.fragment_matter_application_basic_read_vendor_id, container, false);
121116
}
122117

123118
@Override
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
package com.matter.casting;
22

33
import android.util.Log;
4-
54
import com.matter.casting.core.CastingPlayer;
65
import com.matter.casting.core.Endpoint;
7-
86
import java.util.List;
97

10-
/**
11-
* A utility that selects an endpoint based on some criterion
12-
*/
8+
/** A utility that selects an endpoint based on some criterion */
139
public class EndpointSelectorExample {
14-
private static final String TAG = EndpointSelectorExample.class.getSimpleName();
15-
private static final Integer SAMPLE_ENDPOINT_VID = 65521;
10+
private static final String TAG = EndpointSelectorExample.class.getSimpleName();
11+
private static final Integer SAMPLE_ENDPOINT_VID = 65521;
1612

17-
public static Endpoint selectEndpointByVID(CastingPlayer selectedCastingPlayer) {
18-
Endpoint endpoint = null;
19-
if (selectedCastingPlayer != null) {
20-
List<Endpoint> endpoints = selectedCastingPlayer.getEndpoints();
21-
if (endpoints == null) {
22-
Log.e(TAG, "No Endpoints found on CastingPlayer");
23-
} else {
24-
endpoint =
25-
endpoints
26-
.stream()
27-
.filter(e -> SAMPLE_ENDPOINT_VID.equals(e.getVendorId()))
28-
.findFirst()
29-
.get();
30-
}
31-
}
32-
return endpoint;
13+
public static Endpoint selectEndpointByVID(CastingPlayer selectedCastingPlayer) {
14+
Endpoint endpoint = null;
15+
if (selectedCastingPlayer != null) {
16+
List<Endpoint> endpoints = selectedCastingPlayer.getEndpoints();
17+
if (endpoints == null) {
18+
Log.e(TAG, "No Endpoints found on CastingPlayer");
19+
} else {
20+
endpoint =
21+
endpoints
22+
.stream()
23+
.filter(e -> SAMPLE_ENDPOINT_VID.equals(e.getVendorId()))
24+
.findFirst()
25+
.get();
26+
}
3327
}
28+
return endpoint;
29+
}
3430
}

examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/MediaPlaybackSubscribeToCurrentStateExampleFragment.java

+109-101
Original file line numberDiff line numberDiff line change
@@ -24,117 +24,125 @@
2424
import android.view.View;
2525
import android.view.ViewGroup;
2626
import android.widget.TextView;
27-
2827
import androidx.annotation.Nullable;
2928
import androidx.fragment.app.Fragment;
30-
29+
import chip.devicecontroller.ChipClusters;
3130
import com.R;
3231
import com.matter.casting.core.CastingApp;
3332
import com.matter.casting.core.CastingPlayer;
3433
import com.matter.casting.core.Endpoint;
35-
3634
import java.util.Date;
3735

38-
import chip.devicecontroller.ChipClusters;
39-
40-
/** A {@link Fragment} to subscribe to CurrentState (from MediaPLayback cluster) using the TV Casting App. */
36+
/**
37+
* A {@link Fragment} to subscribe to CurrentState (from MediaPLayback cluster) using the TV Casting
38+
* App.
39+
*/
4140
public class MediaPlaybackSubscribeToCurrentStateExampleFragment extends Fragment {
42-
private static final String TAG = MediaPlaybackSubscribeToCurrentStateExampleFragment.class.getSimpleName();
43-
44-
private final CastingPlayer selectedCastingPlayer;
45-
46-
private View.OnClickListener subscribeButtonClickListener;
47-
private View.OnClickListener shutdownSubscriptionsButtonClickListener;
48-
49-
public MediaPlaybackSubscribeToCurrentStateExampleFragment(CastingPlayer selectedCastingPlayer) {
50-
this.selectedCastingPlayer = selectedCastingPlayer;
51-
}
52-
53-
/**
54-
* Use this factory method to create a new instance of this fragment using the provided
55-
* parameters.
56-
*
57-
* @param selectedCastingPlayer CastingPlayer that the casting app connected to
58-
* @return A new instance of fragment MediaPlaybackSubscribeToCurrentStateExampleFragment.
59-
*/
60-
public static MediaPlaybackSubscribeToCurrentStateExampleFragment newInstance(
61-
CastingPlayer selectedCastingPlayer) {
62-
return new MediaPlaybackSubscribeToCurrentStateExampleFragment(selectedCastingPlayer);
63-
}
64-
65-
@Override
66-
public void onCreate(Bundle savedInstanceState) {
67-
super.onCreate(savedInstanceState);
41+
private static final String TAG =
42+
MediaPlaybackSubscribeToCurrentStateExampleFragment.class.getSimpleName();
43+
44+
private final CastingPlayer selectedCastingPlayer;
45+
46+
private View.OnClickListener subscribeButtonClickListener;
47+
private View.OnClickListener shutdownSubscriptionsButtonClickListener;
48+
49+
public MediaPlaybackSubscribeToCurrentStateExampleFragment(CastingPlayer selectedCastingPlayer) {
50+
this.selectedCastingPlayer = selectedCastingPlayer;
51+
}
52+
53+
/**
54+
* Use this factory method to create a new instance of this fragment using the provided
55+
* parameters.
56+
*
57+
* @param selectedCastingPlayer CastingPlayer that the casting app connected to
58+
* @return A new instance of fragment MediaPlaybackSubscribeToCurrentStateExampleFragment.
59+
*/
60+
public static MediaPlaybackSubscribeToCurrentStateExampleFragment newInstance(
61+
CastingPlayer selectedCastingPlayer) {
62+
return new MediaPlaybackSubscribeToCurrentStateExampleFragment(selectedCastingPlayer);
63+
}
64+
65+
@Override
66+
public void onCreate(Bundle savedInstanceState) {
67+
super.onCreate(savedInstanceState);
68+
}
69+
70+
@Override
71+
public View onCreateView(
72+
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
73+
Endpoint endpoint = EndpointSelectorExample.selectEndpointByVID(selectedCastingPlayer);
74+
if (endpoint == null) {
75+
Log.e(TAG, "No Endpoint with sample vendorID found on CastingPlayer");
76+
return inflater.inflate(
77+
R.layout.fragment_matter_media_playback_subscribe_current_state, container, false);
6878
}
6979

70-
@Override
71-
public View onCreateView(
72-
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
73-
Endpoint endpoint = EndpointSelectorExample.selectEndpointByVID(selectedCastingPlayer);
74-
if (endpoint == null) {
80+
this.subscribeButtonClickListener =
81+
v -> {
82+
// get ChipClusters.MediaPlaybackCluster from the endpoint
83+
ChipClusters.MediaPlaybackCluster cluster =
84+
endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class);
85+
if (cluster == null) {
7586
Log.e(
76-
TAG,
77-
"No Endpoint with sample vendorID found on CastingPlayer");
78-
return inflater.inflate(R.layout.fragment_matter_media_playback_subscribe_current_state, container, false);
79-
}
80-
81-
this.subscribeButtonClickListener =
82-
v -> {
83-
// get ChipClusters.MediaPlaybackCluster from the endpoint
84-
ChipClusters.MediaPlaybackCluster cluster =
85-
endpoint.getCluster(ChipClusters.MediaPlaybackCluster.class);
86-
if (cluster == null) {
87-
Log.e(
88-
TAG,
89-
"Could not get ApplicationBasicCluster for endpoint with ID: " + endpoint.getId());
90-
return;
91-
}
92-
93-
// call subscribeCurrentStateAttribute on the cluster object while passing in a
94-
// ChipClusters.IntegerAttributeCallback and [0, 1] for min and max interval params
95-
cluster.subscribeCurrentStateAttribute(new ChipClusters.IntegerAttributeCallback() {
96-
@Override
97-
public void onSuccess(int value) {
98-
Log.d(TAG, "Read success on subscription. Value: " + value + " @ " + new Date());
99-
new Handler(Looper.getMainLooper())
100-
.post(
101-
() -> {
102-
TextView currentStateResult = getView().findViewById(R.id.currentStateResult);
103-
currentStateResult.setText(
104-
"Current State result\nValue: " + value );
105-
});
106-
}
107-
108-
@Override
109-
public void onError(Exception error) {
110-
Log.e(TAG, "Read failure on subscription: " + error);
111-
new Handler(Looper.getMainLooper())
112-
.post(
113-
() -> {
114-
TextView currentStateResult = getView().findViewById(R.id.currentStateResult);
115-
currentStateResult.setText("Current State result\nError: " + error);
116-
});
117-
}
118-
}, 0, 1);
119-
};
120-
121-
this.shutdownSubscriptionsButtonClickListener =
122-
new View.OnClickListener() {
123-
@Override
124-
public void onClick(View v) {
125-
Log.d(TAG, "Shutting down subscriptions");
126-
CastingApp.getInstance().shutdownAllSubscriptions();
127-
}
128-
};
129-
130-
return inflater.inflate(R.layout.fragment_matter_media_playback_subscribe_current_state, container, false);
131-
}
132-
133-
@Override
134-
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
135-
super.onViewCreated(view, savedInstanceState);
136-
Log.d(TAG, "MediaPlaybackSubscribeToCurrentStateExampleFragment.onViewCreated called");
137-
getView().findViewById(R.id.subscribeToCurrentStateButton).setOnClickListener(subscribeButtonClickListener);
138-
getView().findViewById(R.id.shutdownSubscriptionsButton).setOnClickListener(shutdownSubscriptionsButtonClickListener);
139-
}
87+
TAG,
88+
"Could not get ApplicationBasicCluster for endpoint with ID: " + endpoint.getId());
89+
return;
90+
}
91+
92+
// call subscribeCurrentStateAttribute on the cluster object while passing in a
93+
// ChipClusters.IntegerAttributeCallback and [0, 1] for min and max interval params
94+
cluster.subscribeCurrentStateAttribute(
95+
new ChipClusters.IntegerAttributeCallback() {
96+
@Override
97+
public void onSuccess(int value) {
98+
Log.d(TAG, "Read success on subscription. Value: " + value + " @ " + new Date());
99+
new Handler(Looper.getMainLooper())
100+
.post(
101+
() -> {
102+
TextView currentStateResult =
103+
getView().findViewById(R.id.currentStateResult);
104+
currentStateResult.setText("Current State result\nValue: " + value);
105+
});
106+
}
107+
108+
@Override
109+
public void onError(Exception error) {
110+
Log.e(TAG, "Read failure on subscription: " + error);
111+
new Handler(Looper.getMainLooper())
112+
.post(
113+
() -> {
114+
TextView currentStateResult =
115+
getView().findViewById(R.id.currentStateResult);
116+
currentStateResult.setText("Current State result\nError: " + error);
117+
});
118+
}
119+
},
120+
0,
121+
1);
122+
};
123+
124+
this.shutdownSubscriptionsButtonClickListener =
125+
new View.OnClickListener() {
126+
@Override
127+
public void onClick(View v) {
128+
Log.d(TAG, "Shutting down subscriptions");
129+
CastingApp.getInstance().shutdownAllSubscriptions();
130+
}
131+
};
132+
133+
return inflater.inflate(
134+
R.layout.fragment_matter_media_playback_subscribe_current_state, container, false);
135+
}
136+
137+
@Override
138+
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
139+
super.onViewCreated(view, savedInstanceState);
140+
Log.d(TAG, "MediaPlaybackSubscribeToCurrentStateExampleFragment.onViewCreated called");
141+
getView()
142+
.findViewById(R.id.subscribeToCurrentStateButton)
143+
.setOnClickListener(subscribeButtonClickListener);
144+
getView()
145+
.findViewById(R.id.shutdownSubscriptionsButton)
146+
.setOnClickListener(shutdownSubscriptionsButtonClickListener);
147+
}
140148
}

0 commit comments

Comments
 (0)