diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index 94a25f7..6564d52 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 90005f9..53e3bae 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -6,6 +6,7 @@
+
time, temp, speed, image;
@@ -79,6 +91,7 @@ protected void onCreate(Bundle savedInstanceState) {
loaderProgressBar = (ProgressBar)findViewById(R.id.loaderProgressBar);
contentRelativeLayout = (RelativeLayout)findViewById(R.id.contentRelativeLayout);
+ noConnectionRelativeLayout = (RelativeLayout)findViewById(R.id.noConnectionRelativeLayout);
searchView = (SearchView)findViewById(R.id.searchView);
degreeTextView = (TextView)findViewById(R.id.deqreeTextView);
conditionTextView = (TextView)findViewById(R.id.conditionTextView);
@@ -89,6 +102,10 @@ protected void onCreate(Bundle savedInstanceState) {
backgroundImageView = (ImageView)findViewById(R.id.backgroundImageView);
localTimeTextView = (TextView)findViewById(R.id.localTimeTextView);
+ btnTryAgain = (Button)findViewById(R.id.button);
+
+ handler = new Handler();
+
recyclerView = (RecyclerView)findViewById(R.id.cardsRecycleView);
recyclerView.setHasFixedSize(true);
layoutManagerRecycleView = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
@@ -102,27 +119,10 @@ protected void onCreate(Bundle savedInstanceState) {
// Used for controlling location updates
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- // Check permission for location api
- if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
- PackageManager.PERMISSION_GRANTED &&
- ActivityCompat.checkSelfPermission(this,android.Manifest.permission.
- ACCESS_COARSE_LOCATION) !=
- PackageManager.PERMISSION_GRANTED){
- // Ask for permission
- ActivityCompat.requestPermissions(MainActivity.this,
- new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
- android.Manifest.permission.ACCESS_COARSE_LOCATION},
- PERMISSION_CODE);
- }
+ // Request location permissions from device and access location
+ getLocation();
- try {
- /* This takes last location provided by network, if not have no information about location,
- will be returned null */
- location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- }
- catch (Exception ex){
- Log.i("PERMISSION ERROR", ex.getMessage());
- }
+ checkConnection();
if(location != null)
cityName = localization.getCityName(location.getLongitude(), location.getLatitude(),
@@ -139,6 +139,7 @@ public boolean onQueryTextSubmit(String query) {
contentRelativeLayout.setVisibility(View.GONE);
loaderProgressBar.setVisibility(View.VISIBLE);
+ cityName = query;
getAndShowWeatherData(weatherAPI.requestUrl(query));
return false;
}
@@ -148,6 +149,16 @@ public boolean onQueryTextChange(String newText) {
return false;
}
});
+
+ btnTryAgain.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ noConnectionRelativeLayout.setVisibility(View.GONE);
+ loaderProgressBar.setVisibility(View.VISIBLE);
+ getLocation();
+ getAndShowWeatherData(weatherAPI.requestUrl(cityName));
+ }
+ });
}
// Handle user choice
@@ -164,83 +175,131 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
}
}
- void getAndShowWeatherData(String url){
- RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
- formatter = new Formatter();
-
- JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
- new Response.Listener() {
- @Override
- public void onResponse(JSONObject response) {
- // Hide progress bar.
- loaderProgressBar.setVisibility(View.GONE);
- // Show app content
- contentRelativeLayout.setVisibility(View.VISIBLE);
- try {
- // Get JSON objects from server response
- JSONObject location = response.getJSONObject("location");
- JSONObject current = response.getJSONObject("current");
- JSONObject forecastday = response.getJSONObject("forecast").getJSONArray("forecastday")
- .getJSONObject(0);
- JSONArray hour = forecastday.getJSONArray("hour");
-
- // Setting data to all views.
- cityTextView.setText(location.getString("name"));
- degreeTextView.setText(current.getString("temp_c")+"°c");
- conditionTextView.setText(current.getJSONObject("condition").getString("text"));
- regionTextView.setText(location.getString("region"));
- countryTextView.setText(location.getString("country"));
- localTimeTextView.setText(formatter.dateTimeFormatting(location.getString("localtime")));
-
- // Load the image from url using picasso library.
- if(current.getInt("is_day") == 0)
- Picasso.with(MainActivity.this).load(nightBG).into(backgroundImageView);
- else
- Picasso.with(MainActivity.this).load(dayBG).into(backgroundImageView);
-
- Picasso.with(MainActivity.this).load("https:"+current.getJSONObject("condition")
- .getString("icon"))
- .into(conditionIconImageView);
-
- time = new ArrayList<>();
- temp = new ArrayList<>();
- speed = new ArrayList<>();
- image = new ArrayList<>();
-
- for(int i = 0;i < hour.length();i++){
- JSONObject hourObj = hour.getJSONObject(i);
- String timeObj = hourObj.getString("time");
- String temperature = hourObj.getString("temp_c");
- String imageObj = "https:"+hourObj.getJSONObject("condition").getString("icon");
- String wind = hourObj.getString("wind_kph");
-
- time.add(timeObj);
- temp.add(temperature);
- speed.add(wind);
- image.add(imageObj);
- }
-
- adapterRecycleView = new CardView(time, temp, speed, image);
- recyclerView.setAdapter(adapterRecycleView);
-
- } catch (JSONException e) {
-
- e.printStackTrace();
- }
- }
- }, new Response.ErrorListener() {
+ void getLocation(){
+ // Check permission for location api
+ if(ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
+ PackageManager.PERMISSION_GRANTED &&
+ ActivityCompat.checkSelfPermission(this,android.Manifest.permission.
+ ACCESS_COARSE_LOCATION) !=
+ PackageManager.PERMISSION_GRANTED){
+ // Ask for permission
+ ActivityCompat.requestPermissions(MainActivity.this,
+ new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
+ android.Manifest.permission.ACCESS_COARSE_LOCATION},
+ PERMISSION_CODE);
+ }
- @Override
- public void onErrorResponse(VolleyError error) {
+ try {
+ /* This takes last location provided by network, if not have no information about location,
+ will be returned null */
+ location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
+ }
+ catch (Exception ex){
+ Log.i("PERMISSION ERROR", ex.getMessage());
+ }
+ }
- loaderProgressBar.setVisibility(View.GONE);
- contentRelativeLayout.setVisibility(View.VISIBLE);
+ void checkConnection() {
+ // ConnectivityManager for connection checking
+ connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
+ // Check the internet connection
+ connected = Connection.checkConnection(connectivityManager);
+ Log.i("ACCESS NETWORK STATE", "Connected: " + connected);
+ }
- Toast.makeText(MainActivity.this, "No matching location found.", Toast.LENGTH_LONG).show();
+ void getAndShowWeatherData(final String url){
+ checkConnection();
+
+ handler = new Handler();
+ handler.postDelayed(new Runnable() {
+ public void run() {
+ if(connected){
+ RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
+ formatter = new Formatter();
+
+ JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
+ new Response.Listener() {
+ @Override
+ public void onResponse(JSONObject response) {
+ // Hide progress bar.
+ loaderProgressBar.setVisibility(View.GONE);
+ // Show app content
+ contentRelativeLayout.setVisibility(View.VISIBLE);
+ try {
+ // Get JSON objects from server response
+ JSONObject location = response.getJSONObject("location");
+ JSONObject current = response.getJSONObject("current");
+ JSONObject forecastday = response.getJSONObject("forecast").getJSONArray("forecastday")
+ .getJSONObject(0);
+ JSONArray hour = forecastday.getJSONArray("hour");
+
+ // Setting data to all views.
+ cityTextView.setText(location.getString("name"));
+ degreeTextView.setText(current.getString("temp_c")+"°c");
+ conditionTextView.setText(current.getJSONObject("condition").getString("text"));
+ regionTextView.setText(location.getString("region"));
+ countryTextView.setText(location.getString("country"));
+ localTimeTextView.setText(formatter.dateTimeFormatting(location.getString("localtime")));
+
+ // Load the image from url using picasso library.
+ if(current.getInt("is_day") == 0)
+ Picasso.with(MainActivity.this).load(nightBG).into(backgroundImageView);
+ else
+ Picasso.with(MainActivity.this).load(dayBG).into(backgroundImageView);
+
+ Picasso.with(MainActivity.this).load("https:"+current.getJSONObject("condition")
+ .getString("icon"))
+ .into(conditionIconImageView);
+
+ time = new ArrayList<>();
+ temp = new ArrayList<>();
+ speed = new ArrayList<>();
+ image = new ArrayList<>();
+
+ for(int i = 0;i < hour.length();i++){
+ JSONObject hourObj = hour.getJSONObject(i);
+ String timeObj = hourObj.getString("time");
+ String temperature = hourObj.getString("temp_c");
+ String imageObj = "https:"+hourObj.getJSONObject("condition").getString("icon");
+ String wind = hourObj.getString("wind_kph");
+
+ time.add(timeObj);
+ temp.add(temperature);
+ speed.add(wind);
+ image.add(imageObj);
+ }
+
+ adapterRecycleView = new CardView(time, temp, speed, image);
+ recyclerView.setAdapter(adapterRecycleView);
+
+ } catch (JSONException e) {
+
+ e.printStackTrace();
+ }
+ }
+ }, new Response.ErrorListener() {
+
+ @Override
+ public void onErrorResponse(VolleyError error) {
+ loaderProgressBar.setVisibility(View.GONE);
+ contentRelativeLayout.setVisibility(View.VISIBLE);
+ Toast.makeText(MainActivity.this, "No matching location found.", Toast.LENGTH_LONG).show();
+ }
+ });
+
+ queue.add(jsonObjectRequest);
+ }
+ else{
+ loaderProgressBar.setVisibility(View.GONE);
+ noConnectionRelativeLayout.setVisibility(View.VISIBLE);
+ Toast.makeText(getApplicationContext(), "Unable to get weather information.", Toast.LENGTH_LONG).show();
+ }
}
- });
-
- queue.add(jsonObjectRequest);
+ }, 2000); //2 seconds
+ }
+ @Override
+ protected void onResume() {
+ super.onResume();
}
}
diff --git a/app/src/main/java/com/miguelf03kai/weatherapp/utils/Connection.java b/app/src/main/java/com/miguelf03kai/weatherapp/utils/Connection.java
new file mode 100644
index 0000000..c29cbb2
--- /dev/null
+++ b/app/src/main/java/com/miguelf03kai/weatherapp/utils/Connection.java
@@ -0,0 +1,16 @@
+package com.miguelf03kai.weatherapp.utils;
+
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+
+public class Connection {
+
+ // Check if the device have internet connection
+ public static boolean checkConnection(ConnectivityManager connectivityManager){
+ boolean connected = (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
+ .getState() == NetworkInfo.State.CONNECTED || connectivityManager
+ .getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED);
+
+ return connected;
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 2e28d77..01335ad 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -168,4 +168,40 @@
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/mipmap-hdpi/ic_no_connection.png b/app/src/main/res/mipmap-hdpi/ic_no_connection.png
new file mode 100644
index 0000000..21ee25e
Binary files /dev/null and b/app/src/main/res/mipmap-hdpi/ic_no_connection.png differ
diff --git a/app/src/main/res/mipmap-mdpi/ic_no_connection.png b/app/src/main/res/mipmap-mdpi/ic_no_connection.png
new file mode 100644
index 0000000..02be513
Binary files /dev/null and b/app/src/main/res/mipmap-mdpi/ic_no_connection.png differ
diff --git a/app/src/main/res/mipmap-xhdpi/ic_no_connection.png b/app/src/main/res/mipmap-xhdpi/ic_no_connection.png
new file mode 100644
index 0000000..90c10d8
Binary files /dev/null and b/app/src/main/res/mipmap-xhdpi/ic_no_connection.png differ
diff --git a/app/src/main/res/mipmap-xxhdpi/ic_no_connection.png b/app/src/main/res/mipmap-xxhdpi/ic_no_connection.png
new file mode 100644
index 0000000..7d35539
Binary files /dev/null and b/app/src/main/res/mipmap-xxhdpi/ic_no_connection.png differ
diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_no_connection.png b/app/src/main/res/mipmap-xxxhdpi/ic_no_connection.png
new file mode 100644
index 0000000..f67cbac
Binary files /dev/null and b/app/src/main/res/mipmap-xxxhdpi/ic_no_connection.png differ