LocationGetter 생성

This commit is contained in:
JUNGGWAN KIM 2024-09-30 08:47:25 +09:00
parent e872fe4e8a
commit 6e34a6cad6
4 changed files with 83 additions and 172 deletions

View File

@ -113,6 +113,7 @@ import rasel.lunar.launcher.workers.ContactInfoGetter
import rasel.lunar.launcher.workers.DCGetter
import rasel.lunar.launcher.workers.DotaxGetter
import rasel.lunar.launcher.workers.FmKoreaGetter
import rasel.lunar.launcher.workers.LocationGetter
import rasel.lunar.launcher.workers.NewsFeedsGetter
import rasel.lunar.launcher.workers.OpenWeatherGetter
import rasel.lunar.launcher.workers.RecentCallGetter
@ -296,6 +297,17 @@ internal class LauncherActivity : AppCompatActivity() {
.build())
}, delay, TimeUnit.SECONDS)
val weatherDelay = 3L
Executors.newSingleThreadScheduledExecutor().schedule({
//de574a260b1f474d99955729241909
mWorkManager?.cancelAllWorkByTag(LocationGetter.TAG)
mWorkManager?.enqueueUniquePeriodicWork(
LocationGetter.TAG, ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
PeriodicWorkRequestBuilder<OpenWeatherGetter>(midTimePeriod, TimeUnit.MINUTES)
.addTag(LocationGetter.TAG)
.build())
}, weatherDelay, TimeUnit.SECONDS)
Executors.newSingleThreadScheduledExecutor().schedule({
//de574a260b1f474d99955729241909
mWorkManager?.cancelAllWorkByTag(OpenWeatherGetter.TAG)
@ -304,8 +316,7 @@ internal class LauncherActivity : AppCompatActivity() {
PeriodicWorkRequestBuilder<OpenWeatherGetter>(midTimePeriod, TimeUnit.MINUTES)
.addTag(OpenWeatherGetter.TAG)
.build())
}, 3, TimeUnit.SECONDS)
}, weatherDelay + 3, TimeUnit.SECONDS)
}
fun workmanager() : WorkManager? {

View File

@ -1,109 +0,0 @@
package rasel.lunar.launcher.model
import android.app.appsearch.StorageInfo
import com.google.gson.annotations.SerializedName
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.Ignore
//class WeatherInfo : RealmObject {
// constructor()
//// constructor(coord: Loc?, weather: Array<Weather>?, main: Main?) {
//// this.coord = coord
//// weather?.let {
//// this.weather?.addAll(it)
//// }
////
//// this.main = main
//// }
//
// @SerializedName("coord")
// var coord: Loc? = null
//
// @Ignore
// @SerializedName("weather")
// var weather: ArrayList<Weather>? = arrayListOf()
//
// var weathers: RealmList<Weather> = realmListOf()
//
// @SerializedName("base")
// var base: String? = null
//
// @SerializedName("main")
// var main: Main? = null
//fun filled() {
// weather?.let {
// weathers.addAll(it)
// }
//
//}
//
// @SerializedName("visibility")
// var visibility: String? = null
//
// @SerializedName("wind")
// var wind: Wind? = null
//
// @SerializedName("clouds")
// var clouds: Clouds? = null
//
// @SerializedName("dt")
// var dt: String? = null
//
// @SerializedName("sys")
// var sys: Sys? = null
//
// @SerializedName("timezone")
// var timezone: String? = null
//
// @SerializedName("id")
// var id: String? = null
//
// @SerializedName("name")
// var name: String? = null
//
// @SerializedName("cod")
// var cod: String? = null
// }
class Loc : RealmObject {
var lon: String? = null
var lat: String? = null
}
class Weather : RealmObject {
var id: String? = null
var main: String? = null
var description: String? = null
var icon: String? = null
}
class Main : RealmObject {
var temp: String? = null
var feels_like: String? = null
var temp_min: String? = null
var temp_max: String? = null
var pressure: String? = null
var humidity: String? = null
var sea_level: String? = null
var grnd_level: String? = null
}
class Wind: RealmObject {
var speed: String? = null
var deg: String? = null
var gust: String? = null
}
class Clouds: RealmObject {
var all: String? = null
}
class Sys: RealmObject {
var type: String? = null
var id: String? = null
var country: String? = null
var sunrise: String? = null
var sunset: String? = null
}

View File

@ -0,0 +1,39 @@
package rasel.lunar.launcher.workers
import android.annotation.SuppressLint
import android.content.Context
import android.location.Location
import androidx.work.WorkerParameters
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationTokenSource
import rasel.lunar.launcher.utils.BLog
class LocationGetter(context: Context, workerParams: WorkerParameters) : BaseGetter(context, workerParams) {
companion object {
val TAG = "LocationGetter"
var longitude: Double? = null
var latitude: Double? = null
}
@SuppressLint("MissingPermission")
override fun realWork(): Result {
BLog.LOGE("${OpenWeatherGetter.TAG} realWork()")
LocationServices.getFusedLocationProviderClient(this.applicationContext)
.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY, CancellationTokenSource().token)
.addOnSuccessListener{ success: Location? ->
success?.let {
BLog.LOGE("Location >>> $it")
BLog.LOGE("Location >>> (latitude)${it.longitude}/(longitude)${it.latitude}")
longitude = it.longitude
latitude = it.latitude
}
}.addOnFailureListener{
BLog.LOGE("Location error >>> $it")
}
return Result.success()
}
}

View File

@ -24,9 +24,6 @@ class OpenWeatherGetter(context: Context, workerParams: WorkerParameters) : Base
val TAG = "OpenWeatherGetter"
}
//////////////////////////////////////////
// openWeatherMap
val URI_OPEN_WEATHER_MAP = "https://api.openweathermap.org"
val KEY_OPEN_WEATHER_MAP = "87cd0810b7e4b4debd31a6ef98b98154"
// weatherapi
val VER_WEATHERAPI = "v1"
val URI_WEATHERAPI = "https://api.weatherapi.com"
@ -36,73 +33,46 @@ class OpenWeatherGetter(context: Context, workerParams: WorkerParameters) : Base
var lon: Double? = null // 경도
var lat: Double? = null // 위도
//////////////////////////////////////////
@SuppressLint("MissingPermission")
override fun realWork(): Result {
BLog.LOGE("${TAG} realWork() ")
// setLocation()
fun location(loc: Location?) {
val service = Retrofit.Builder()
.baseUrl(URI_WEATHERAPI)
.addConverterFactory(GsonConverterFactory.create())
.build()
.run { this.create<RestrofitService>() }
service.getForecast(
ver = VER_WEATHERAPI,
key = KEY_WEATHERAPI,
q = "${loc?.latitude ?: lat},${loc?.longitude ?: lon}",
days = DAYS.toString()
)?.execute()?.let { response ->
response.body()?.let { weatherInfo ->
BLog.LOGE("Location error >>> $weatherInfo")
WorkersDb.getRealm().writeBlocking {
weatherInfo.readyForSaving()
copyToRealm(weatherInfo, UpdatePolicy.ALL)
}
BLog.LOGE("saved weatherForcast >>> ${WorkersDb.getRealm().query<WeatherForcast>().first().find()}")
}
}
BLog.LOGE("${TAG} realWork()")
// 위치 값 가져오기
lat = LocationGetter.latitude
lon = LocationGetter.longitude
if (lat != null && lon != null) {
getWeather(lat!!, lon!!)
} else {
BLog.LOGE("lat or lon is null")
}
val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this.applicationContext)
fusedLocationProviderClient.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY,CancellationTokenSource().token)
.addOnSuccessListener{ success: Location? ->
success?.let {
BLog.LOGE("Location >>> $it")
BLog.LOGE("Location >>> (latitude)${it.longitude}/(longitude)${it.latitude}")
lon = it.longitude
lat = it.latitude
//{https://home.openweathermap.org/api 에서 정보를 조회 하자}
location(null)
}
}.addOnFailureListener{
BLog.LOGE("Location error >>> $it")
}
// }
return Result.success()
}
@SuppressLint("MissingPermission")
fun setLocation() {
}
fun getWeather(loc: Location?) {
fun getWeather(latitude: Double, longitude: Double) {
Retrofit.Builder()
.baseUrl(URI_WEATHERAPI)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create<RestrofitService>()
.getForecast( // weatherApi
ver = VER_WEATHERAPI,
key = KEY_WEATHERAPI,
q = "$latitude,$longitude",
days = DAYS.toString()
)?.execute()?.let { response ->
BLog.LOGE("weatherApi forecast response >>> $response")
response.body()?.let { weatherInfo ->
// Realm에 저장
WorkersDb.getRealm().writeBlocking {
weatherInfo.readyForSaving()
copyToRealm(weatherInfo, UpdatePolicy.ALL)
}
BLog.LOGE("saved weatherForcast >>> ${WorkersDb.getRealm().query<WeatherForcast>().first().find()}")
}
}
}
}
interface RestrofitService {
// open_weather_map
// @GET("/data/2.5/weather")
// fun getPosts(
// @Query("lat") lat: Double,
// @Query("lon") lon: Double,
// @Query("appid") key: String
// ): Call<WeatherInfo?>?
// weather_api
@GET("/{ver}/forecast.json")
fun getForecast(