-
Notifications
You must be signed in to change notification settings - Fork 0
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
SatohAyaka
wants to merge
2
commits into
master
Choose a base branch
from
ayaka-forgroundService
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
求改善方法 #50
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
app/src/main/java/net/harutiro/trainalert2/features/location/repository/LocationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
servicoIntentの綴りが違います