Skip to content

Commit

Permalink
Merge pull request #10 from akamfoad/crash_reporter_feature
Browse files Browse the repository at this point in the history
Crash reporter feature added and Weather fragment landscape mode added
  • Loading branch information
akamfoad authored May 15, 2020
2 parents 2b42584 + 22ecbc2 commit 81de6b6
Show file tree
Hide file tree
Showing 12 changed files with 385 additions and 21 deletions.
18 changes: 17 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 29
Expand All @@ -21,6 +22,13 @@ android {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
// apply plugin: 'com.android.application'
//
// apply plugin: 'com.google.gms.google-services' // Google Services Gradle plugin

// Apply the Crashlytics Gradle plugin
apply plugin: 'com.google.firebase.crashlytics'

}

dependencies {
Expand Down Expand Up @@ -52,5 +60,13 @@ dependencies {
// GSON parser to json
implementation 'com.google.code.gson:gson:2.8.6'
// Youtube player api
implementation files('F:\\2019-2020\\Mobile Application - Dr. Moayad\\Assignement\\YouTubeAndroidPlayerApi-1.2.2\\libs\\YouTubeAndroidPlayerApi.jar')
implementation files('./libs/YouTubeAndroidPlayerApi.jar')

// firebase crash reporter
// Recommended: Add the Firebase SDK for Google Analytics.
implementation 'com.google.firebase:firebase-analytics:17.4.1'

// Add the Firebase Crashlytics SDK.
implementation 'com.google.firebase:firebase-crashlytics:17.0.0'

}
40 changes: 40 additions & 0 deletions app/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"project_info": {
"project_number": "179828873900",
"firebase_url": "https://soran-travel.firebaseio.com",
"project_id": "soran-travel",
"storage_bucket": "soran-travel.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:179828873900:android:4a02e274f3660839c69d09",
"android_client_info": {
"package_name": "com.fanaye.sorantravel"
}
},
"oauth_client": [
{
"client_id": "179828873900-b1mrfe6lqnukq6j2k0fefl8s6aa94rdi.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyAN66mD1SebNN9UKNYY8GItNe_xUvRnjVg"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "179828873900-b1mrfe6lqnukq6j2k0fefl8s6aa94rdi.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
Binary file added app/libs/YouTubeAndroidPlayerApi.jar
Binary file not shown.
124 changes: 109 additions & 15 deletions app/src/main/java/com/fanaye/sorantravel/ui/weather/WeatherFragment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.fanaye.sorantravel.ui.weather;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
Expand All @@ -17,44 +20,80 @@

import com.fanaye.sorantravel.R;
import com.fanaye.sorantravel.ui.weather.model.DATA;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.gson.Gson;

import java.util.Locale;

import static androidx.core.content.ContextCompat.getSystemService;
import static java.lang.String.format;

public class WeatherFragment extends Fragment {
private final String cityID = "92002";

private ImageView condIcon;
JSONWeatherTask jsonWeatherTask;
private ProgressBar progressBar;
private TextView conditionDesc;
private TextView temp;
private TextView press;
private TextView internetErrorLabel;
private TextView humidity;
private TextView pressLabel;
private TextView windSpeed;
private TextView windDeg;
private TextView humidityLabel;
private byte[] imageData;
private TextView windLabel;
private FirebaseAnalytics crashAnalysis;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.weather_fragment, container, false);
crashAnalysis = FirebaseAnalytics.getInstance(getContext());
if (getResources().getConfiguration().getLocales().get(0).getLanguage().equalsIgnoreCase("ku")) {
root.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
internetErrorLabel = root.findViewById(R.id.internet_error_label);
progressBar = root.findViewById(R.id.progressBar);
progressBar.setIndeterminate(true);
condIcon = root.findViewById(R.id.cond_icon);
conditionDesc = root.findViewById(R.id.conditionDesc);
temp = root.findViewById(R.id.temp);
press = root.findViewById(R.id.pressure);
pressLabel = root.findViewById(R.id.pressure_label);
humidity = root.findViewById(R.id.humidity);
humidityLabel = root.findViewById(R.id.humidity_label);
windSpeed = root.findViewById(R.id.wind_speed);
windDeg = root.findViewById(R.id.wind_deg);
windLabel = root.findViewById(R.id.wind_label);

if (internetAvailable(getContext())) {
jsonWeatherTask = new JSONWeatherTask();
jsonWeatherTask.execute(cityID);
} else {
condIcon.setImageResource(R.drawable.ic_signal_wifi_off_black_24dp);
conditionDesc.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
temp.setVisibility(View.GONE);
press.setVisibility(View.GONE);
pressLabel.setVisibility(View.GONE);
humidityLabel.setVisibility(View.GONE);
windLabel.setVisibility(View.GONE);
humidity.setVisibility(View.GONE);
windSpeed.setVisibility(View.GONE);
windDeg.setVisibility(View.GONE);
internetErrorLabel.setVisibility(View.VISIBLE);
}

root.setOnClickListener(v -> {
progressBar.setVisibility(View.VISIBLE);
jsonWeatherTask = new JSONWeatherTask();
jsonWeatherTask.execute(cityID);
});


final String cityID = "92002";
JSONWeatherTask jsonWeatherTask = new JSONWeatherTask();
jsonWeatherTask.execute(cityID);
return root;
}

Expand All @@ -63,40 +102,95 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}

private boolean internetAvailable(Context c) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;

ConnectivityManager cm = (ConnectivityManager) getSystemService(c, ConnectivityManager.class);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}

private class JSONWeatherTask extends AsyncTask<String, Integer, DATA> {

@Override
protected DATA doInBackground(String... params) {

if (!internetAvailable(getContext())) {
cancel(false);
}
publishProgress(10);
String json = ((new WeatherHttpClient()).getWeatherData(params[0], getString(R.string.WEATHER_API_KEY)));
String json = ((new WeatherHttpClient(crashAnalysis)).getWeatherData(params[0], getString(R.string.WEATHER_API_KEY)));

publishProgress(30);
Gson gson = new Gson();
publishProgress(50);
DATA data = gson.fromJson(json, DATA.class);
DATA data = null;
if (json != null) {
data = gson.fromJson(json, DATA.class);
}
publishProgress(75);
imageData = ((new WeatherHttpClient()).getImage(data.getWeather().get(0).getIcon()));
if (data != null) {
imageData = ((new WeatherHttpClient(crashAnalysis)).getImage(data.getWeather().get(0).getIcon()));
}
publishProgress(100);
return data;
}

@Override
protected void onCancelled(DATA data) {
condIcon.setImageResource(R.drawable.ic_signal_wifi_off_black_24dp);
conditionDesc.setVisibility(View.GONE);
conditionDesc.setText(R.string.NO_INTERNET_CONNECTION);
progressBar.setVisibility(View.GONE);
temp.setVisibility(View.GONE);
press.setVisibility(View.GONE);
pressLabel.setVisibility(View.GONE);
humidityLabel.setVisibility(View.GONE);
windLabel.setVisibility(View.GONE);
humidity.setVisibility(View.GONE);
windSpeed.setVisibility(View.GONE);
windDeg.setVisibility(View.GONE);
internetErrorLabel.setVisibility(View.VISIBLE);
}

@Override
protected void onPostExecute(DATA data) {
super.onPostExecute(data);
progressBar.setVisibility(View.GONE);
if (imageData != null && imageData.length > 0) {
Bitmap img = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
condIcon.setImageBitmap(img);
} else {
condIcon.setImageResource(R.drawable.ic_signal_wifi_off_black_24dp);
}
if (data != null) {
internetErrorLabel.setVisibility(View.GONE);
conditionDesc.setVisibility(View.VISIBLE);
temp.setVisibility(View.VISIBLE);
press.setVisibility(View.VISIBLE);
pressLabel.setVisibility(View.VISIBLE);
humidityLabel.setVisibility(View.VISIBLE);
windLabel.setVisibility(View.VISIBLE);
humidity.setVisibility(View.VISIBLE);
windSpeed.setVisibility(View.VISIBLE);
windDeg.setVisibility(View.VISIBLE);

conditionDesc.setText(format(Locale.getDefault(), "%s(%s)", data.getWeather().get(0).getMain(), data.getWeather().get(0).getDescription()));
temp.setText(format(Locale.ENGLISH, "%d°C", Math.round((data.getMain().getTemp()))));
humidity.setText(String.format(Locale.ENGLISH, "%d%%", Math.round(data.getMain().getHumidity())));
press.setText(String.format(Locale.ENGLISH, "%d hPa", Math.round(data.getMain().getPressure())));
windSpeed.setText(String.format("%s mps", data.getWind().getSpeed()));
windDeg.setText(String.format(Locale.ENGLISH, "%d°", data.getWind().getDeg()));
}

conditionDesc.setText(format(Locale.getDefault(), "%s(%s)", data.getWeather().get(0).getMain(), data.getWeather().get(0).getDescription()));
temp.setText(format(Locale.ENGLISH, "%d°C", Math.round((data.getMain().getTemp()))));
humidity.setText(String.format(Locale.ENGLISH, "%d%%", Math.round(data.getMain().getHumidity())));
press.setText(String.format(Locale.ENGLISH, "%d hPa", Math.round(data.getMain().getPressure())));
windSpeed.setText(String.format("%s mps", data.getWind().getSpeed()));
windDeg.setText(String.format(Locale.ENGLISH, "%d°", data.getWind().getDeg()));
}
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.fanaye.sorantravel.ui.weather;

import android.os.Bundle;

import com.google.firebase.analytics.FirebaseAnalytics;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -13,7 +17,11 @@ public class WeatherHttpClient {

private static String BASE_URL = "http://api.openweathermap.org/data/2.5/weather?id=";
private static String IMG_URL = "http://openweathermap.org/img/wn/";
private FirebaseAnalytics crashAnalysis;

WeatherHttpClient(FirebaseAnalytics crashAnalysis) {
this.crashAnalysis = crashAnalysis;
}

public String getWeatherData(String location, String key) {
HttpURLConnection con = null;
Expand All @@ -24,7 +32,14 @@ public String getWeatherData(String location, String key) {
}
try {
con = (HttpURLConnection) (new URL(BASE_URL + location + "&APPID=" + key + "&units=metric&lang=" + lang)).openConnection();
con.connect();
try {
con.connect();
} catch (IOException e) {
Bundle WAPIREQERR_DATA = new Bundle();
WAPIREQERR_DATA.putString("Message", e.getMessage());
System.out.println(e.getMessage());
crashAnalysis.logEvent("Weather_API_req_error", WAPIREQERR_DATA);
}

// Let's read the response
StringBuffer buffer = new StringBuffer();
Expand Down Expand Up @@ -69,9 +84,11 @@ public byte[] getImage(String code) {
baos.write(buffer);

return baos.toByteArray();
} catch (IOException t) {
t.printStackTrace(System.err);
// t.printStackTrace();
} catch (IOException e) {
Bundle WAPIREQERR_DATA = new Bundle();
WAPIREQERR_DATA.putString("Message", e.getMessage());
System.out.println(e.getMessage());
crashAnalysis.logEvent("Weather_IMAGE_req_error", WAPIREQERR_DATA);
} finally {
try {
is.close();
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_signal_wifi_off_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#80000000"
android:pathData="M23.64,7c-0.45,-0.34 -4.93,-4 -11.64,-4 -1.5,0 -2.89,0.19 -4.15,0.48L18.18,13.8 23.64,7zM17.04,15.22L3.27,1.44 2,2.72l2.05,2.06C1.91,5.76 0.59,6.82 0.36,7l11.63,14.49 0.01,0.01 0.01,-0.01 3.9,-4.86 3.32,3.32 1.27,-1.27 -3.46,-3.46z" />
</vector>
Loading

0 comments on commit 81de6b6

Please sign in to comment.