diff --git a/app/src/main/java/com/example/hpostesting/presentation/dashboard/ui/LoginFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/dashboard/ui/LoginFragment.kt index a0dc629..f351ad7 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/dashboard/ui/LoginFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/dashboard/ui/LoginFragment.kt @@ -13,15 +13,16 @@ package com.example.hpostesting.presentation.dashboard.ui +import android.Manifest import android.annotation.SuppressLint import android.content.Context -import android.content.Context.WIFI_SERVICE import android.content.SharedPreferences +import android.content.pm.PackageManager import android.location.Location import android.location.LocationListener import android.location.LocationManager import android.net.ConnectivityManager -import android.net.wifi.WifiManager +import android.net.NetworkInfo import android.os.Build import android.os.Bundle import android.util.Log @@ -30,21 +31,25 @@ import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.annotation.RequiresApi +import androidx.core.app.ActivityCompat +import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat.getSystemService import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController import com.example.hpostesting.data.constant.Constants +import com.google.android.gms.location.FusedLocationProviderClient +import com.google.android.gms.location.LocationCallback +import com.google.android.gms.location.LocationRequest +import com.google.android.gms.location.LocationResult +import com.google.android.gms.location.LocationServices import `in`.sminnovations.hpostesting.R import `in`.sminnovations.hpostesting.databinding.FragmentLoginBinding -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch -import java.net.HttpURLConnection -import java.net.URL -import java.util.Scanner +import java.net.NetworkInterface class LoginFragment : Fragment() { + private val LOCATION_PERMISSION_REQUEST_CODE = 1001 + private lateinit var locationManager: LocationManager var latitude = 0.0 var longitude = 0.0 var TAG = "LoginFragmentCheck" @@ -65,7 +70,31 @@ class LoginFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) init() - checkLocation() + locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager + + // Check for location permission + if (ContextCompat.checkSelfPermission(requireContext(), + Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(requireContext(), + Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { + // Permission is not granted, request it + ActivityCompat.requestPermissions(requireActivity(), + arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION), + LOCATION_PERMISSION_REQUEST_CODE) + } else { + // Permission is granted, fetch location + checkLocation() + } + + if (isNetworkProviderAvailable()) { + Toast.makeText(requireContext(), "Network provider is available", Toast.LENGTH_SHORT).show() + } else { + Toast.makeText(requireContext(), "Network provider is not available", Toast.LENGTH_SHORT).show() + } + } + + private fun isNetworkProviderAvailable(): Boolean { + + return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) } private fun init() { if (isUserLoggedIn()) { @@ -129,49 +158,52 @@ class LoginFragment : Fragment() { /** Check if we can get our location */ @SuppressLint("MissingPermission") fun checkLocation() { - // Get the location manager + val locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager val locationProviderWifi = LocationManager.NETWORK_PROVIDER - val locationProviderGPS = LocationManager.GPS_PROVIDER + val locationProviderGPS = LocationManager.FUSED_PROVIDER val locationListenerNetwork: LocationListener val locationListenerGPS: LocationListener var gps_enabled = false var network_enabled = false + Log.d(TAG,"PRoveider"+locationManager.allProviders.toString()) + //check wifi //check wifi val connectivityManager = requireActivity().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager val mWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) + if (mWifi!!.isConnected) { Log.d(TAG, "Wifi connected") } + //check gps and wifi availability + //check gps and wifi availability try { gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) - } catch (ex: Exception) { + } catch (ex: java.lang.Exception) { } + try { - network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) - } catch (ex: Exception) { + network_enabled = + locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) + } catch (ex: java.lang.Exception) { } + if (!gps_enabled) { Log.d(TAG, "GPS: Missing") - }else{ -// getPublicIpAddr { ipAddress -> -// // Do something with the ipAddress -// Log.d(TAG, "GPS: PRESENT"+ipAddress) -// } } if (!network_enabled) { Log.d(TAG, "Network: Missing") } + /* Location change listeners */ + /* Location change listeners */try { - Log.d(TAG, "GPS: PRESENT TRy") // Define a listener that responds to gps location updates locationListenerGPS = object : LocationListener { override fun onLocationChanged(location: Location) { - Log.d(TAG, "GPS: PRESENT loc") Log.d( TAG, "GPS: Latitude: " + location.latitude + ", Longitude = " + location.longitude @@ -179,22 +211,27 @@ class LoginFragment : Fragment() { } override fun onStatusChanged(provider: String, status: Int, extras: Bundle) { - Log.d(TAG, "GP location found 1") + Log.d(TAG, "GPS location found 1") } override fun onProviderEnabled(provider: String) { - Log.d(TAG, "GP location found 2") + Log.d(TAG, "GPS location found 2") } override fun onProviderDisabled(provider: String) { - Log.d(TAG, "GP location found 3") + Log.d(TAG, "GPS location found 3") } } - locationManager.requestLocationUpdates(locationProviderGPS, 0, 0f, locationListenerGPS) - Log.d(TAG, "GPS: PRESENT Req") - } catch (e: Exception) { - Log.e(TAG, "Location Exception: " + e.message) + locationManager.requestLocationUpdates( + locationProviderGPS, + 0, + 0f, + locationListenerGPS + ) + } catch (e: java.lang.Exception) { + Log.e(TAG, "Location Exception GPS: " + e.message) } + try { // Define a listener that responds to wifi location updates locationListenerNetwork = object : LocationListener { @@ -223,8 +260,22 @@ class LoginFragment : Fragment() { 0f, locationListenerNetwork ) - } catch (e: Exception) { - Log.e(TAG, "NW Location Exception: " + e.message) + } catch (e: java.lang.Exception) { + Log.e(TAG, "Location Exception: " + e.message) + } + + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) { + if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + // Permission granted, fetch location + checkLocation() + } else { + // Permission denied + Toast.makeText(requireContext(), "Location permission denied", Toast.LENGTH_SHORT).show() + } } } // fun getPublicIpAddr(callback: (String) -> Unit) { diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt index e0c96e7..1ab8eef 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt @@ -245,24 +245,24 @@ class HemoCubeFragment : Fragment() { } else { handleReadingFinish() } -// hemoCubeViewModel.fireBaseUpload.observe(viewLifecycleOwner) { result -> -// if (result == "Success") { -// uploadedToCloud = true -// var accessToken = sharedPreferences.getString(Constants.ACCESS_TOKEN, "").toString() -// showToast(R.string.test_upload) -// -// } -// if (result == "Local") { -// showToast(R.string.internt_not_local) -// startActivity(Intent(requireActivity(), DashboardActivity::class.java)) -// } -// if (result == "Error") { -// showToast(R.string.error_local) -// startActivity(Intent(requireActivity(), DashboardActivity::class.java)) -// } -// -// binding.progressBar.visibility = View.GONE -// } + hemoCubeViewModel.fireBaseUpload.observe(viewLifecycleOwner) { result -> + if (result == "Success") { + uploadedToCloud = true + var accessToken = sharedPreferences.getString(Constants.ACCESS_TOKEN, "").toString() + showToast(R.string.test_upload) + + } + if (result == "Local") { + showToast(R.string.internt_not_local) + startActivity(Intent(requireActivity(), DashboardActivity::class.java)) + } + if (result == "Error") { + showToast(R.string.error_local) + startActivity(Intent(requireActivity(), DashboardActivity::class.java)) + } + + binding.progressBar.visibility = View.GONE + } hemoCubeViewModel.getDeviceData(sharedPreferences.getString(Constants.USER_ID, ""))