1414import android .widget .ImageView ;
1515import android .widget .TextView ;
1616
17- import java .io .BufferedInputStream ;
17+ import com .squareup .okhttp .ResponseBody ;
18+
1819import java .io .IOException ;
19- import java .io .InputStream ;
20- import java .net .HttpURLConnection ;
21- import java .net .URL ;
20+ import java .net .SocketTimeoutException ;
2221
2322import butterknife .Bind ;
2423import butterknife .ButterKnife ;
24+ import retrofit .Call ;
25+ import retrofit .Callback ;
26+ import retrofit .Response ;
27+ import retrofit .Retrofit ;
2528
2629public class CctvFragment extends Fragment {
2730
@@ -41,59 +44,17 @@ public class CctvFragment extends Fragment {
4144 private boolean mIsChangingOrientation = false ;
4245 private Bitmap mImage ;
4346
44- private class StreamingTask extends AsyncTask <Void , Void , ResponseDetails > {
45- int cameraId ;
46-
47- @ Override
48- protected void onPreExecute () {
49- super .onPreExecute ();
50- cameraId = mCameraId ;
51- }
52-
53- @ Override
54- protected ResponseDetails doInBackground (Void ... params ) {
55- try {
56- return stream (getActivity (), mStationId , mCameraId );
57- } catch (IOException e ) {
58- e .printStackTrace ();
59- }
60- return null ;
61- }
62-
63- @ Override
64- protected void onPostExecute (ResponseDetails responseDetails ) {
65- super .onPostExecute (responseDetails );
66- //If has internet connection.
67- if (responseDetails != null && responseDetails .isConnected ()) {
68- //If request hasn't changed while waiting for a response.
69- if (responseDetails .getStationId () == mStationId
70- && responseDetails .getCameraId () == mCameraId ) {
71- //if the request was successful and responded with an image.
72- if (responseDetails .getResponseCode () == 200
73- && responseDetails .getImage () != null ) {
74- showImage (responseDetails .getImage ());
75- } else if (responseDetails .getResponseCode () == 408 ) {//If connection timeout.
76- clearImage ();
77- displayMessage (R .string .connection_timeout );
78- } else {//If request failed.
79- clearImage ();
80- displayMessage (R .string .no_cctv );
81- }
82- } else { //If the request has changed while waiting, keep loading.
83- showLoading ();
84- }
85-
86- } else { //If no internet connection.
87- displayMessage (R .string .no_connection );
88- }
89- new StreamingTask ().execute ();
90- }
91- }
47+ private ApiService mApiService ;
48+ private Call <ResponseBody > mCaller ;
9249
9350 @ Override
9451 public void onCreate (Bundle savedInstanceState ) {
9552 super .onCreate (savedInstanceState );
9653 setRetainInstance (true );
54+
55+ String baseUrl = getResources ().getString (R .string .api_base_url );
56+ Retrofit retrofit = new Retrofit .Builder ().baseUrl (baseUrl ).build ();
57+ mApiService = retrofit .create (ApiService .class );
9758 }
9859
9960 @ Override
@@ -116,7 +77,102 @@ public void onResume() {
11677 } else {
11778 showLoading ();
11879 }
119- new StreamingTask ().execute ();
80+ stream (mStationId , mCameraId );
81+ }
82+
83+ private void stream (final int stationId , final int cameraId ) {
84+ if (isResumed ()) {
85+ //If has internet connection.
86+ if (isConnected (getActivity ())) {
87+ mCaller = mApiService .stream (stationId , cameraId );
88+ mCaller .enqueue (new Callback <ResponseBody >() {
89+ @ Override
90+ public void onResponse (Response <ResponseBody > response ) {
91+ //If request hasn't changed while waiting for a response.
92+ if (stationId == mStationId && cameraId == mCameraId ) {
93+ //if the request was successful and responded with an image.
94+ if (response .code () == 200 && response .body () != null ) {
95+ try {
96+ mImage = BitmapFactory .decodeStream (response .body ().byteStream ());
97+ if (mImage != null ) {
98+ showImage (mImage );
99+ } else {//If didn't return an image.
100+ clearImage ();
101+ displayMessage (R .string .cctv_not_available );
102+ }
103+
104+ } catch (IOException e ) {
105+ e .printStackTrace ();
106+ }
107+
108+ } else if (response .code () == 408 ) {//If connection timeout.
109+ clearImage ();
110+ displayMessage (R .string .connection_timeout );
111+ } else {//If request failed.
112+ clearImage ();
113+ displayMessage (R .string .cctv_not_available );
114+ }
115+
116+ } else {//If the request has changed while waiting, keep loading.
117+ showLoading ();
118+ }
119+
120+ stream (mStationId , mCameraId );
121+ }
122+
123+ @ Override
124+ public void onFailure (Throwable t ) {
125+ if (t instanceof SocketTimeoutException ) {
126+ clearImage ();
127+ displayMessage (R .string .connection_timeout );
128+ } else {
129+ clearImage ();
130+ displayMessage (R .string .cctv_not_available );
131+ }
132+ stream (mStationId , mCameraId );
133+ }
134+ });
135+
136+ } else {//If no internet connection.
137+ displayMessage (R .string .connection_not_available );
138+ waitForAvailableNetwork ();
139+ }
140+ }
141+ }
142+
143+ private boolean isConnected (Context context ) {
144+ if (context != null ) {
145+ ConnectivityManager connMgr = (ConnectivityManager )
146+ context .getSystemService (Context .CONNECTIVITY_SERVICE );
147+
148+ if (connMgr != null ) {
149+ NetworkInfo networkInfo = connMgr .getActiveNetworkInfo ();
150+ if (networkInfo != null ) {
151+ return networkInfo .isConnected ();
152+ }
153+ }
154+ }
155+ return false ;
156+ }
157+
158+ private void waitForAvailableNetwork () {
159+ new AsyncTask <Void , Void , Void >() {
160+ @ Override
161+ protected Void doInBackground (Void ... params ) {
162+ while (!isConnected (getActivity ())) {
163+ if (mCaller != null ) {
164+ mCaller .cancel ();
165+ }
166+ }
167+ return null ;
168+ }
169+
170+ @ Override
171+ protected void onPostExecute (Void aVoid ) {
172+ super .onPostExecute (aVoid );
173+ stream (mStationId , mCameraId );
174+ }
175+ }.execute ();
120176 }
121177
122178 @ Override
@@ -143,7 +199,7 @@ public void onDetach() {
143199
144200 private void showImage (Bitmap image ) {
145201 if (image == null ) {
146- throw new IllegalArgumentException ("image cannot be null" );
202+ throw new IllegalArgumentException ("image cannot be null. " );
147203 } else {
148204 mCctvStatus .setVisibility (View .INVISIBLE );
149205 mProgressBar .setVisibility (View .INVISIBLE );
@@ -154,9 +210,13 @@ private void showImage(Bitmap image) {
154210
155211 public void setCamera (int stationId , int cameraId ) {
156212 if (mStationId != stationId || mCameraId != cameraId ) {
157- showLoading ();
213+ if (mCaller != null ) {
214+ mCaller .cancel ();
215+ }
158216 mStationId = stationId ;
159217 mCameraId = cameraId ;
218+ showLoading ();
219+ stream (mStationId , mCameraId );
160220 }
161221 }
162222
@@ -190,56 +250,4 @@ private void displayMessage(int stringResId) {
190250 mImageView .setImageBitmap (null );
191251 }
192252 }
193-
194- private boolean isConnected (Context context ) {
195- if (context != null ) {
196- ConnectivityManager connMgr = (ConnectivityManager )
197- context .getSystemService (Context .CONNECTIVITY_SERVICE );
198-
199- if (connMgr != null ) {
200- NetworkInfo networkInfo = connMgr .getActiveNetworkInfo ();
201- if (networkInfo != null ) {
202- return networkInfo .isConnected ();
203- }
204- }
205- }
206- return false ;
207- }
208-
209- private ResponseDetails stream (Context context , int stationId , int cameraId )
210- throws IOException {
211- ResponseDetails responseDetails = new ResponseDetails (stationId , cameraId );
212-
213- if (isConnected (context )) {
214- responseDetails .setIsConnected (true );
215- InputStream is = null ;
216-
217- try {
218- URL url = new URL ("http://api.pinoymobileapps.com/mrtcctv/?stationId="
219- + stationId + "&cameraId=" + cameraId );
220- HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
221- conn .connect ();
222-
223- responseDetails .setResponseCode (conn .getResponseCode ());
224- if (responseDetails .getResponseCode () == 200 ) {
225- is = conn .getInputStream ();
226- BufferedInputStream bufferedInputStream = new BufferedInputStream (is );
227- Bitmap image = BitmapFactory .decodeStream (bufferedInputStream );
228- responseDetails .setImage (image );
229- }
230-
231- } finally {
232- if (is != null ) {
233- is .close ();
234- }
235- }
236-
237- } else {
238- responseDetails .setIsConnected (false );
239- }
240-
241- return responseDetails ;
242- }
243-
244-
245253}
0 commit comments