Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit f0e62dc

Browse files
committed
Implemented cancel previous request when changing camera.
Enable immersive mode only on devices with Kitkat or greater. Changed minimum supported version to Gingerbread. Changed messages and layout attributes for displaying status. Uses the new api.
1 parent 8c8a36d commit f0e62dc

File tree

7 files changed

+156
-166
lines changed

7 files changed

+156
-166
lines changed

app/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ android {
66

77
defaultConfig {
88
applicationId "com.pinoymobileapps.mrtcctv"
9-
minSdkVersion 15
9+
minSdkVersion 10
1010
targetSdkVersion 23
1111
versionCode 7
12-
versionName "2.1.2"
12+
versionName "2.2.0"
1313
}
1414
buildTypes {
1515
release {
@@ -24,5 +24,6 @@ dependencies {
2424
compile 'com.android.support:appcompat-v7:23.0.1'
2525
compile 'com.android.support:design:23.0.1'
2626
compile 'com.jakewharton:butterknife:7.0.1'
27-
compile group: 'commons-io', name: 'commons-io', version: '2.0.1'
27+
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
28+
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
2829
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.pinoymobileapps.mrtcctv;
2+
3+
import com.squareup.okhttp.ResponseBody;
4+
5+
import retrofit.Call;
6+
import retrofit.http.GET;
7+
import retrofit.http.Query;
8+
9+
public interface ApiService {
10+
@GET("/mrtcctv2/")
11+
Call<ResponseBody> stream(@Query("stationId") int stationId, @Query("cameraId") int cameraId);
12+
}
13+

app/src/main/java/com/pinoymobileapps/mrtcctv/CctvFragment.java

Lines changed: 115 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
import android.widget.ImageView;
1515
import android.widget.TextView;
1616

17-
import java.io.BufferedInputStream;
17+
import com.squareup.okhttp.ResponseBody;
18+
1819
import java.io.IOException;
19-
import java.io.InputStream;
20-
import java.net.HttpURLConnection;
21-
import java.net.URL;
20+
import java.net.SocketTimeoutException;
2221

2322
import butterknife.Bind;
2423
import butterknife.ButterKnife;
24+
import retrofit.Call;
25+
import retrofit.Callback;
26+
import retrofit.Response;
27+
import retrofit.Retrofit;
2528

2629
public 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
}

app/src/main/java/com/pinoymobileapps/mrtcctv/MainActivity.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.pinoymobileapps.mrtcctv;
22

3+
import android.content.Context;
34
import android.content.res.Configuration;
5+
import android.os.Build;
46
import android.os.Bundle;
57
import android.support.annotation.Nullable;
8+
import android.support.v4.content.ContextCompat;
69
import android.support.v4.view.GravityCompat;
710
import android.support.v4.widget.DrawerLayout;
811
import android.support.v7.app.ActionBarDrawerToggle;
@@ -149,7 +152,9 @@ public void onWindowFocusChanged(boolean hasFocus) {
149152
super.onWindowFocusChanged(hasFocus);
150153
if (hasFocus
151154
&& getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
152-
&& mCctvFragment != null) {
155+
&& mCctvFragment != null
156+
&& mCctvFragment.getView() != null
157+
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
153158
mCctvFragment.getView().setSystemUiVisibility(
154159
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
155160
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
@@ -300,7 +305,7 @@ private void changeStation(final int stationId) {
300305
private void selectButton(Button button) {
301306
clearButtons();
302307
if (button != null) {
303-
button.setTextColor(getResources().getColor(R.color.button_selected_text));
308+
button.setTextColor(ContextCompat.getColor(this, R.color.button_selected_text));
304309
}
305310
}
306311

@@ -313,7 +318,7 @@ private void clearButtons() {
313318

314319
private void clearButton(Button button) {
315320
if (button != null) {
316-
button.setTextColor(getResources().getColor(R.color.button_default_text));
321+
button.setTextColor(ContextCompat.getColor(this, R.color.button_default_text));
317322
}
318323
}
319324

app/src/main/java/com/pinoymobileapps/mrtcctv/ResponseDetails.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)