33import android .content .Context ;
44import android .net .Uri ;
55import android .os .Bundle ;
6+ import android .util .Log ;
67import android .view .LayoutInflater ;
78import android .view .View ;
89import android .view .ViewGroup ;
10+ import android .widget .Button ;
11+ import android .widget .EditText ;
912import android .widget .FrameLayout ;
1013import android .widget .ImageButton ;
1114import android .widget .TextView ;
15+ import android .widget .Toast ;
1216
1317import androidx .annotation .NonNull ;
1418import androidx .annotation .Nullable ;
19+ import androidx .appcompat .app .AlertDialog ;
1520import androidx .appcompat .content .res .AppCompatResources ;
1621import androidx .lifecycle .ViewModelProvider ;
1722
2227import com .example .layout_version .MainTab .Streaming .Recorder .Recorder ;
2328import com .example .layout_version .MainTab .Streaming .Recorder .RecordingState ;
2429import com .example .layout_version .MainTab .Streaming .Recorder .RecordingStateChangeListener ;
30+ import com .example .layout_version .Network .NetworkRequestManager ;
2531import com .example .layout_version .R ;
2632
33+ import org .json .JSONException ;
34+ import org .json .JSONObject ;
35+
36+ import java .util .Map ;
37+
2738public class StreamingFragment extends StateFragment <RecordingState > {
2839 private Context context ;
2940 private StreamingViewModel streamingViewModel ;
@@ -35,8 +46,12 @@ public class StreamingFragment extends StateFragment<RecordingState> {
3546 private ImageButton recordingButton ;
3647 private ImageButton streamRefreshButton ;
3748
49+ private ImageButton optionButton ;
50+ private ImageButton shareButton ;
3851 private Recorder recorder ;
3952
53+ private boolean optionFlag ;
54+
4055 @ Override
4156 public View onCreateView (LayoutInflater inflater , ViewGroup container ,
4257 Bundle savedInstanceState ) {
@@ -59,8 +74,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
5974 deviceStatusView = layout .findViewById (R .id .deviceStatusView );
6075 recordingButton = layout .findViewById (R .id .recordingButton );
6176 streamRefreshButton = layout .findViewById (R .id .streamRefreshButton );
77+ optionButton = layout .findViewById (R .id .optionButton );
78+ shareButton = layout .findViewById (R .id .shareButton );
6279 recorder = new Recorder (context , streamingViewModel );
6380
81+ hideOptions ();
82+
6483 return layout ;
6584 }
6685
@@ -87,6 +106,16 @@ else if(status == RecordingState.FAILED)
87106 update (streamingViewModel .getSelectedItem ().getValue ());
88107 });
89108
109+ optionButton .setOnClickListener (view1 -> {
110+ if (optionFlag )
111+ hideOptions ();
112+ else
113+ showOptions ();
114+ });
115+
116+ shareButton .setOnClickListener (view1 -> {
117+ showShareDialog ();
118+ });
90119 }
91120
92121 public void update (@ Nullable ChannelItem channel )
@@ -114,4 +143,79 @@ public void onDestroy() {
114143 streamingPlayer .removeListener (playerListener );
115144 streamingPlayer .release ();
116145 }
146+
147+ public void hideOptions ()
148+ {
149+ optionFlag = false ;
150+ optionButton .setImageDrawable (AppCompatResources .getDrawable (context , R .drawable .baseline_vert_options_24 ));
151+ recordingButton .setVisibility (View .INVISIBLE );
152+ shareButton .setVisibility (View .INVISIBLE );
153+ }
154+
155+ public void showOptions ()
156+ {
157+ optionFlag = true ;
158+ optionButton .setImageDrawable (AppCompatResources .getDrawable (context , R .drawable .baseline_close_24 ));
159+ recordingButton .setVisibility (View .VISIBLE );
160+ shareButton .setVisibility (View .VISIBLE );
161+ }
162+
163+ public void showShareDialog ()
164+ {
165+ View view = getActivity ().getLayoutInflater ().inflate (R .layout .device_share , null );
166+ TextView codeText = view .findViewById (R .id .codeTextView );
167+ EditText codeEditText = view .findViewById (R .id .codeEdit );
168+ Button connectButton = view .findViewById (R .id .connectButton );
169+ Button shareButton = view .findViewById (R .id .shareButton );
170+
171+ codeText .setVisibility (View .INVISIBLE );
172+ codeEditText .setText ("" );
173+
174+ connectButton .setOnClickListener (view1 -> {
175+ Log .e ("Connect" , "here" );
176+
177+ Log .e ("Share" , "here" );
178+ NetworkRequestManager nrm = new NetworkRequestManager (context );
179+ JSONObject jsonObject = new JSONObject (Map .of (
180+ "token" , Account .getInstance ().getTokenData ().getValue (),
181+ "code" , codeEditText .getText ().toString ()
182+ ));
183+ nrm .Post (R .string .device_code_endpoint , jsonObject ,
184+ json -> {
185+ Toast .makeText (context , "Successfully connected device to account" , Toast .LENGTH_SHORT ).show ();
186+ },
187+ json ->{
188+ Toast .makeText (context , "Error" , Toast .LENGTH_SHORT ).show ();
189+ });
190+ });
191+
192+ shareButton .setOnClickListener (view1 -> {
193+ NetworkRequestManager nrm = new NetworkRequestManager (context );
194+ JSONObject jsonObject = new JSONObject (Map .of (
195+ "token" , Account .getInstance ().getTokenData ().getValue (),
196+ "device_id" , streamingViewModel .getSelectedItem ().getValue ().getDeviceId ()
197+ ));
198+ nrm .Post (R .string .device_share_endpoint , jsonObject ,
199+ json -> {
200+ String code = null ;
201+ try {
202+ code = json .getString ("code" );
203+ codeText .setText (code );
204+ } catch (JSONException e ) {
205+ codeText .setText ("Error" );
206+ }
207+ codeText .setVisibility (View .VISIBLE );
208+ },
209+ json ->{
210+ codeText .setText ("Error" );
211+ codeText .setVisibility (View .VISIBLE );
212+ });
213+ });
214+
215+ new AlertDialog .Builder (getActivity ())
216+ .setView (view )
217+ .show ();
218+ }
219+
220+
117221}
0 commit comments