summaryrefslogtreecommitdiffstats
path: root/app/src
diff options
context:
space:
mode:
Diffstat (limited to 'app/src')
-rw-r--r--app/src/main/java/com/jopek/kanakaeiwa/FirstFragment.java96
-rw-r--r--app/src/main/java/com/jopek/kanakaeiwa/SecondFragment.java38
-rw-r--r--app/src/main/res/drawable/ic_baseline_error_24.xml5
3 files changed, 115 insertions, 24 deletions
diff --git a/app/src/main/java/com/jopek/kanakaeiwa/FirstFragment.java b/app/src/main/java/com/jopek/kanakaeiwa/FirstFragment.java
index b0e7699..ccd1187 100644
--- a/app/src/main/java/com/jopek/kanakaeiwa/FirstFragment.java
+++ b/app/src/main/java/com/jopek/kanakaeiwa/FirstFragment.java
@@ -2,6 +2,7 @@ package com.jopek.kanakaeiwa;
import android.Manifest;
import android.annotation.SuppressLint;
+import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
@@ -15,8 +16,9 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
-import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
@@ -40,6 +42,18 @@ public class FirstFragment extends Fragment {
private double lon;
private double lat;
private String cityName = null;
+ private AlertDialog alertLoading = null;
+ private AlertDialog alertError = null;
+
+ private ActivityResultLauncher<String> requestPermissionLauncher =
+ registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
+ Log.d(MainActivity.TAG, "registerForActivityResult: ");
+ if (isGranted) {
+ getLocation();
+ } else {
+ getIPLocation();
+ }
+ });
@Override
public View onCreateView(
@@ -53,10 +67,22 @@ public class FirstFragment extends Fragment {
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
+ AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
+ builder.setMessage("Getting location");
+ builder.setCancelable(false);
+ builder.setNegativeButton(
+ "Try setting location manually",
+ (dialog, id) -> {
+ dialog.cancel();
+ NavHostFragment.findNavController(FirstFragment.this).navigate(R.id.action_FirstFragment_to_SecondFragment);
+ });
+ alertLoading = builder.create();
+
if (requireActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
getLocation();
} else {
- ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
+ requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION);
+// ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
@@ -88,6 +114,7 @@ public class FirstFragment extends Fragment {
binding.minTemp.setText(main.getString("temp_min") + "°C");
binding.pressure.setText(main.getString("pressure") + " hPa");
binding.windSpeed.setText(wind.getString("speed") + " m/s");
+ alertLoading.hide();
} catch (Exception e) {
showLocationError();
e.printStackTrace();
@@ -118,6 +145,8 @@ public class FirstFragment extends Fragment {
@SuppressLint("MissingPermission")
public void getLocation() {
+ alertLoading.show();
+
LocationManager locationManager = (LocationManager) requireActivity().getSystemService(Context.LOCATION_SERVICE);
long youngestTime = 0;
Location youngest = null;
@@ -169,7 +198,8 @@ public class FirstFragment extends Fragment {
boolean providerSet = false;
for (String provider : providers) {
if (locationManager.isProviderEnabled(provider)) {
- locationManager.requestLocationUpdates(provider, MIN_TIME_LOC, MIN_M_LOC, locationListener);
+// locationManager.requestLocationUpdates(provider, MIN_TIME_LOC, MIN_M_LOC, locationListener);
+ locationManager.requestSingleUpdate(provider, locationListener, null);
chosenProvider = provider;
providerSet = true;
break;
@@ -177,35 +207,57 @@ public class FirstFragment extends Fragment {
}
Log.d(MainActivity.TAG, "getBestLocation: " + chosenProvider);
if (!providerSet) {
- Log.d(MainActivity.TAG, "getBestLocation: getting locationByIp");
- chosenProvider = "IP";
- Requests requests = new Requests();
-// requests.get("https://ipapi.co/json", res -> {
- requests.get("https://ipwho.is/", res -> {
- Log.d(MainActivity.TAG, "IP Location found: ");
- try {
- Log.d(MainActivity.TAG, "getBestLocation: " + res);
- JSONObject data = new JSONObject(res);
- lon = data.getDouble("longitude");
- lat = data.getDouble("latitude");
- cityName = data.getString("city");
- locationSet = true;
- getWeather();
- } catch (JSONException e) {
- e.printStackTrace();
- showLocationError();
- }
- });
+ getIPLocation();
}
}
+ public void getIPLocation() {
+ if(!alertLoading.isShowing()) alertLoading.show();
+ Log.d(MainActivity.TAG, "getBestLocation: getting locationByIp");
+ chosenProvider = "IP";
+ Requests requests = new Requests();
+// requests.get("https://ipapi.co/json", res -> {
+ requests.get("https://ipwho.is/", res -> {
+ Log.d(MainActivity.TAG, "IP Location found: ");
+ try {
+ Log.d(MainActivity.TAG, "getBestLocation: " + res);
+ JSONObject data = new JSONObject(res);
+ lon = data.getDouble("longitude");
+ lat = data.getDouble("latitude");
+ cityName = data.getString("city");
+ locationSet = true;
+ getWeather();
+ } catch (JSONException e) {
+ e.printStackTrace();
+ showLocationError();
+ }
+ });
+ }
public void showLocationError() {
+ AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
+ builder.setMessage("Error while getting location");
+ builder.setCancelable(false);
+ builder.setIcon(R.drawable.ic_baseline_error_24);
+ builder.setPositiveButton(
+ "Try again",
+ (dialog, id) -> {
+ dialog.cancel();
+ getLocation();
+ });
+ builder.setNegativeButton(
+ "Set location manually",
+ (dialog, id) -> {
+ dialog.cancel();
+ NavHostFragment.findNavController(FirstFragment.this).navigate(R.id.action_FirstFragment_to_SecondFragment);
+ });
+ builder.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
+ Log.d(MainActivity.TAG, "onRequestPermissionsResult: ");
switch (requestCode) {
case 1:
if (grantResults.length > 0 &&
diff --git a/app/src/main/java/com/jopek/kanakaeiwa/SecondFragment.java b/app/src/main/java/com/jopek/kanakaeiwa/SecondFragment.java
index 398e828..0920330 100644
--- a/app/src/main/java/com/jopek/kanakaeiwa/SecondFragment.java
+++ b/app/src/main/java/com/jopek/kanakaeiwa/SecondFragment.java
@@ -1,6 +1,7 @@
package com.jopek.kanakaeiwa;
import android.Manifest;
+import android.app.AlertDialog;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle;
@@ -29,6 +30,8 @@ public class SecondFragment extends Fragment {
private double lon;
private double lat;
private String cityName = null;
+ private AlertDialog alertLoading = null;
+ private AlertDialog alertError = null;
@Override
@@ -45,18 +48,31 @@ public class SecondFragment extends Fragment {
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
+ AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
+ builder.setMessage("Getting location");
+ builder.setCancelable(false);
+ builder.setNegativeButton(
+ "Try setting location automatically",
+ (dialog, id) -> {
+ dialog.cancel();
+ NavHostFragment.findNavController(SecondFragment.this).navigate(R.id.action_SecondFragment_to_FirstFragment);
+ });
+ alertLoading = builder.create();
+
if (requireActivity().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
binding.buttonFirst.setOnClickListener(view1 -> NavHostFragment.findNavController(SecondFragment.this)
.navigate(R.id.action_SecondFragment_to_FirstFragment));
+
binding.cityBtn.setOnClickListener(view1 -> {
getWeather(String.valueOf(binding.editText.getText()));
});
}
public void getWeather(String cityName) {
+ alertLoading.show();
String url = "https://api.openweathermap.org/data/2.5/weather?appid=d7eea7960e9b5024b8441fb6f9344ce4&units=metric&q=" + cityName;
Requests requests = new Requests();
requests.get(url, res -> {
@@ -66,7 +82,7 @@ public class SecondFragment extends Fragment {
JSONObject main = data.getJSONObject("main");
JSONObject wind = data.getJSONObject("wind");
JSONObject sys = data.getJSONObject("sys");
- requests.setRemoteSrc(binding.weatherImg, "https://openweathermap.org/img/w/" + weather.getString("icon") + ".png");
+ requests.setRemoteSrc(SecondFragment.this.binding.weatherImg, "https://openweathermap.org/img/w/" + weather.getString("icon") + ".png");
if (cityName != null)
binding.cityName.setText(cityName + ", " + sys.getString("country"));
else
@@ -78,6 +94,7 @@ public class SecondFragment extends Fragment {
binding.minTemp.setText(main.getString("temp_min") + "°C");
binding.pressure.setText(main.getString("pressure") + " hPa");
binding.windSpeed.setText(wind.getString("speed") + " m/s");
+ alertLoading.hide();
} catch (Exception e) {
showLocationError();
e.printStackTrace();
@@ -86,7 +103,24 @@ public class SecondFragment extends Fragment {
}
public void showLocationError() {
-
+ Log.d(MainActivity.TAG, "showLocationError: ");
+ alertLoading.hide();
+ AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
+ builder.setMessage("Error while getting location");
+ builder.setCancelable(false);
+ builder.setIcon(R.drawable.ic_baseline_error_24);
+ builder.setPositiveButton(
+ "Try again",
+ (dialog, id) -> {
+ dialog.cancel();
+ });
+ builder.setNegativeButton(
+ "Set location automatically",
+ (dialog, id) -> {
+ dialog.cancel();
+ NavHostFragment.findNavController(SecondFragment.this).navigate(R.id.action_SecondFragment_to_FirstFragment);
+ });
+ builder.show();
}
@Override
diff --git a/app/src/main/res/drawable/ic_baseline_error_24.xml b/app/src/main/res/drawable/ic_baseline_error_24.xml
new file mode 100644
index 0000000..391e8b4
--- /dev/null
+++ b/app/src/main/res/drawable/ic_baseline_error_24.xml
@@ -0,0 +1,5 @@
+<vector android:height="24dp" android:tint="#000000"
+ android:viewportHeight="24" android:viewportWidth="24"
+ android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
+ <path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-2h2v2zM13,13h-2L11,7h2v6z"/>
+</vector>