Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

求改善方法 #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />
<service
android:name=".features.location.repository.LocationService"
android:enabled="true"
android:exported="false" />
</application>
</manifest>
166 changes: 87 additions & 79 deletions app/src/main/java/net/harutiro/trainalert2/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package net.harutiro.trainalert2

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
//import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.maps.model.LatLng
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
//import androidx.lifecycle.lifecycleScope
//import com.google.android.gms.maps.model.LatLng
//import kotlinx.coroutines.Dispatchers
//import kotlinx.coroutines.launch
//import kotlinx.coroutines.withContext
import net.harutiro.trainalert2.core.presenter.FirstPage
import net.harutiro.trainalert2.features.location.entity.CurrentLocationData
import net.harutiro.trainalert2.features.location.repository.DistanceJudgement
import net.harutiro.trainalert2.features.location.repository.LocationRepository
import net.harutiro.trainalert2.features.notification.api.NotificationApi
import net.harutiro.trainalert2.features.room.routeDB.repositories.RouteRepository
//import net.harutiro.trainalert2.features.location.entity.CurrentLocationData
//import net.harutiro.trainalert2.features.location.repository.DistanceJudgement
//import net.harutiro.trainalert2.features.location.repository.LocationRepository
import net.harutiro.trainalert2.features.location.repository.LocationService
//import net.harutiro.trainalert2.features.notification.api.NotificationApi
//import net.harutiro.trainalert2.features.room.routeDB.repositories.RouteRepository
import net.harutiro.trainalert2.ui.theme.TrainAlert2Theme

class MainActivity : ComponentActivity() {

private val routeRepository = RouteRepository()
private var notificationApi: NotificationApi? = null
private val locationRepository = LocationRepository()
// private val routeRepository = RouteRepository()
// private var notificationApi: NotificationApi? = null
// private val locationRepository = LocationRepository()


override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -34,72 +37,77 @@ class MainActivity : ComponentActivity() {
}
}

// NotificationApiのインスタンスを作成
notificationApi = NotificationApi(this)

locationRepository.initLocationApi(this)

// 距離判定の処理
checkDistance()
}

private fun checkDistance() {
val judgerange = 600.0 // 判定距離

locationRepository.currentLocationUpdates { CurrentLocationData ->

Log.d("currentLocation", "$CurrentLocationData")

lifecycleScope.launch(Dispatchers.IO) {
// データベースから目的地の情報を取得
val routeList = routeRepository.loadAllRoutes() // データベースにあるルート全てを取得する
if (routeList.isEmpty()) {
return@launch
}

val currentLocation = CurrentLocationData (
CurrentLocationData.latitude,
CurrentLocationData.longitude
)
Log.d("currentLocation", "$currentLocation")

// 全ルートに対して処理を行う
routeList.forEach { destination ->
// 目的地を取得
val destinationLocation = LatLng(
destination.endLatitude ?: 0.0,
destination.endLongitude ?: 0.0
)

// 距離判定を行う
val isWithinDistance = DistanceJudgement().resultDistance(
currentLocation.latitude, currentLocation.longitude,
destinationLocation.latitude, destinationLocation.longitude,
judgerange // 距離は100m以内か
)

// 判定がtrueの場合、通知を表示
if (isWithinDistance) {
if(destination.alertMethods == 1) {
withContext(Dispatchers.Main) {
notificationApi?.showNotification(
"目的地に近づきました",
"間もなく到着です!"
)
}
}else if(destination.alertMethods == 2) {
withContext(
Dispatchers.Main
) {
notificationApi?.vibrate()
}
}
}
}
}

// サービスの起動
val serviceIntent = Intent(this, LocationService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent)
} else {
startService(serviceIntent)
Comment on lines +40 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

servicoIntentの綴りが違います

}


// // NotificationApiのインスタンスを作成
// notificationApi = NotificationApi(this)
//
// locationRepository.initLocationApi(this)
//
// // 距離判定の処理
// checkDistance()
}
//
// private fun checkDistance() {
// val judgerange = 600.0 // 判定距離
//
// locationRepository.currentLocationUpdates { CurrentLocationData ->
//
// Log.d("currentLocation", "$CurrentLocationData")
//
// lifecycleScope.launch(Dispatchers.IO) {
// // データベースから目的地の情報を取得
// val routeList = routeRepository.loadAllRoutes() // データベースにあるルート全てを取得する
// if (routeList.isEmpty()) {
// return@launch
// }
//
// val currentLocation = CurrentLocationData (
// CurrentLocationData.latitude,
// CurrentLocationData.longitude
// )
// Log.d("currentLocation", "$currentLocation")
//
// // 全ルートに対して処理を行う
// routeList.forEach { destination ->
// // 目的地を取得
// val destinationLocation = LatLng(
// destination.endLatitude ?: 0.0,
// destination.endLongitude ?: 0.0
// )
//
// // 距離判定を行う
// val isWithinDistance = DistanceJudgement().resultDistance(
// currentLocation.latitude, currentLocation.longitude,
// destinationLocation.latitude, destinationLocation.longitude,
// judgerange // 距離は100m以内か
// )
//
// // 判定がtrueの場合、通知を表示
// if (isWithinDistance) {
// if(destination.alertMethods == 1) {
// withContext(Dispatchers.Main) {
// notificationApi?.showNotification(
// "目的地に近づきました",
// "間もなく到着です!"
// )
// }
// }else if(destination.alertMethods == 2) {
// withContext(
// Dispatchers.Main
// ) {
// notificationApi?.vibrate()
// }
// }
// }
// }
// }
// }
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package net.harutiro.trainalert2.features.location.repository

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
//import android.content.Context
import android.content.Intent
import android.os.IBinder
import android.os.Build
import androidx.core.app.NotificationCompat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import net.harutiro.trainalert2.features.location.entity.CurrentLocationData
//import net.harutiro.trainalert2.features.location.repository.DistanceJudgement
//import net.harutiro.trainalert2.features.location.repository.LocationRepository
import net.harutiro.trainalert2.features.notification.api.NotificationApi
import net.harutiro.trainalert2.features.room.routeDB.repositories.RouteRepository
import com.google.android.gms.maps.model.LatLng
import kotlinx.coroutines.DelicateCoroutinesApi
import net.harutiro.trainalert2.MainActivity
import net.harutiro.trainalert2.R

class LocationService : Service() {

private val routeRepository = RouteRepository()
private val notificationApi = NotificationApi(this)
private val locationRepository = LocationRepository()

override fun onCreate() {
super.onCreate()
locationRepository.initLocationApi(this)
startForegroundService()
checkDistance()
}

override fun onBind(intent: Intent?): IBinder? = null

private fun startForegroundService() {
val notificationChannelId = "LocationServiceChannel"
val channelName = "Location Service"

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
notificationChannelId,
channelName,
NotificationManager.IMPORTANCE_LOW
)
val manager = getSystemService(NotificationManager::class.java)
manager?.createNotificationChannel(channel)
}

val notification: Notification = NotificationCompat.Builder(this, notificationChannelId)
.setContentTitle("TrainAlert2")
.setContentText("位置情報を監視中")
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_IMMUTABLE))
.build()

startForeground(1, notification)
}

@OptIn(DelicateCoroutinesApi::class)
private fun checkDistance() {
val judgerange = 600.0 // 判定距離

locationRepository.currentLocationUpdates { currentLocationData ->

GlobalScope.launch(Dispatchers.IO) {
val routeList = routeRepository.loadAllRoutes()
if (routeList.isEmpty()) return@launch

val currentLocation = CurrentLocationData(
currentLocationData.latitude,
currentLocationData.longitude
)

routeList.forEach { destination ->
val destinationLocation = LatLng(
destination.endLatitude ?: 0.0,
destination.endLongitude ?: 0.0
)

val isWithinDistance = DistanceJudgement().resultDistance(
currentLocation.latitude, currentLocation.longitude,
destinationLocation.latitude, destinationLocation.longitude,
judgerange
)

if (isWithinDistance) {
launch(Dispatchers.Main) {
when (destination.alertMethods) {
1 -> notificationApi.showNotification(
"目的地に近づきました",
"間もなく到着です!"
)
2 -> notificationApi.vibrate()
}
}
}
}
}
}
}
}