|
24 | 24 | import android.view.View;
|
25 | 25 | import android.view.ViewGroup;
|
26 | 26 | import android.widget.TextView;
|
27 |
| - |
28 | 27 | import androidx.annotation.Nullable;
|
29 | 28 | import androidx.fragment.app.Fragment;
|
30 |
| - |
| 29 | +import chip.devicecontroller.ChipClusters; |
31 | 30 | import com.R;
|
32 | 31 | import com.matter.casting.core.CastingApp;
|
33 | 32 | import com.matter.casting.core.CastingPlayer;
|
34 | 33 | import com.matter.casting.core.Endpoint;
|
35 |
| - |
36 | 34 | import java.util.Date;
|
37 | 35 |
|
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 | + */ |
41 | 40 | 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); |
68 | 78 | }
|
69 | 79 |
|
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) { |
75 | 86 | 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 | + } |
140 | 148 | }
|
0 commit comments