Skip to content

Commit

Permalink
Merge pull request #20 from tukcomCD2024/18-feat-스마트폰-기기-상태-얻어오기
Browse files Browse the repository at this point in the history
18 feat 스마트폰 기기 상태 얻어오기
  • Loading branch information
kyujin0911 authored Feb 29, 2024
2 parents 44c5af3 + 4407cca commit 44ba306
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 104 deletions.
2 changes: 1 addition & 1 deletion frontend/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,11 @@ data class LocationInfo(
val time: String,
val date: String,
val currentSpeed: Float,
val accelerationsensorX: Float,
val accelerationsensorY: Float,
val accelerationsensorZ: Float,
val gyrosensorX: Float,
val gyrosensorY: Float,
val gyrosensorZ: Float,
val directionsensorX: Float,
val directionsensorY: Float,
val directionsensorZ: Float,
val lightsensor: Float,
val battery: Float,
val accelerationsensor: List<Float>,
val gyrosensor: List<Float>,
val directionsensor: List<Float>,
val lightsensor: List<Float>,
val battery: Int,
val isInternetOn: Boolean,
val isGpsOn: Boolean,
val isRingstoneOn: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ class HomeRepositoryImpl @Inject constructor(
override suspend fun postLocationInfo(request: LocationInfo): NetworkResult<LocationInfoResponse> {
return handleApi({api.postLocationInfo(request)}) {response: LocationInfoResponse -> response}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -199,42 +199,4 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home),
}
}
}

private fun makeDummy(){
lifecycleScope.launch{
while (true){
delay(60000)
val axislast = axisList.last()
val locaLast = locationList.last()
Log.d("last", "$axislast, $locaLast")
val currentDate = Date(System.currentTimeMillis())
val sdf = SimpleDateFormat("YYYY-MM-DD")
val date = sdf.format(currentDate)
Log.d("date", date.toString())
viewModel.postLocationInfo(LocationInfo(
"227609",
locaLast.latitude,
locaLast.longitude,
"지금",
date,
0f,
axislast.xAxis,
axislast.yAxis,
axislast.zAxis,
0f,
0f,
0f,
0f,
0f,
0f,
0f,
80.0f,
true,
true,
true
))
delay(5000)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class HomeViewModel @Inject constructor(
): ViewModel() {
val init: Int = 0

init {
/*init {
lightSensor.startListening()
lightSensor.setOnSensorValuesChangedListener { values ->
val lux = values
Expand All @@ -57,7 +57,7 @@ class HomeViewModel @Inject constructor(
val gyro = values
Log.d("gyro", gyro.toString())
}
}
}*/


fun postLocationInfo(request: LocationInfo){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.location.LocationManager
import android.os.Parcelable
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
Expand All @@ -26,10 +29,14 @@ import kotlin.apply
@AndroidEntryPoint
class MainNokActivity : BaseActivity<ActivityNokMainBinding>(R.layout.activity_nok_main) {
override fun initView() {
//gps off시 gps 설정화면으로 이동
checkGpsStatus()

Intent(applicationContext, LocationService::class.java).apply {
action = LocationService.ACTION_START
startService(this)
}

val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragmentContainer) as NavHostFragment
val navController = navHostFragment.navController
Expand All @@ -51,6 +58,22 @@ class MainNokActivity : BaseActivity<ActivityNokMainBinding>(R.layout.activity_n
)
}

private fun requestSystemGPSEnable() {
Toast.makeText(this, "핸드폰 GPS를 켜주세요", Toast.LENGTH_SHORT).show()
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent)
}

private fun checkGpsStatus(){
val locationManager = this.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val isGpsEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) && locationManager.isProviderEnabled(
LocationManager.GPS_PROVIDER)

if (!isGpsEnable){
requestSystemGPSEnable()
}
}

fun getStatusBarHeight(context: Context): Int {
val resourceId = context.resources.getIdentifier("status_bar_height", "dimen", "android")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package kr.ac.tukorea.whereareu.util

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.location.Location
import android.location.LocationManager
import android.location.LocationRequest
import android.os.Looper
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationResult
Expand All @@ -18,27 +22,27 @@ class DefaultLocationClient(
private val context: Context,
private val client: FusedLocationProviderClient
): LocationClient {
private val locationManager by lazy {
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
}
@SuppressLint("MissingPermission")
override fun getLocationUpdates(interval: Long): Flow<Location> {
return callbackFlow {
if(!context.hasLocationPermission()){
if (!context.hasLocationPermission()) {
throw LocationClient.LocationException("Missing location permission")
}

val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
val isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
if(!isGpsEnabled && !isNetworkEnabled){
if (!getGpsStatus()) {
throw LocationClient.LocationException("GPS is disabled")
}

val request = com.google.android.gms.location.LocationRequest.Builder(interval)
.build()

val locationCallback = object : LocationCallback(){
val locationCallback = object : LocationCallback() {
override fun onLocationResult(result: LocationResult) {
super.onLocationResult(result)
result.locations.lastOrNull()?.let{location ->
result.locations.lastOrNull()?.let { location ->
launch { send(location) }
}
}
Expand All @@ -55,4 +59,7 @@ class DefaultLocationClient(
}
}
}
override fun getGpsStatus(): Boolean {
return locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package kr.ac.tukorea.whereareu.util

import android.location.Location
import android.location.LocationManager
import kotlinx.coroutines.flow.Flow

interface LocationClient {
fun getLocationUpdates(interval: Long): Flow<Location>

fun getGpsStatus(): Boolean

class LocationException(message: String): Exception()
}
Loading

0 comments on commit 44ba306

Please sign in to comment.