From 8fff21a6d8c6749986b34f03ca5b0b26c672ce67 Mon Sep 17 00:00:00 2001 From: lunaticbum <> Date: Tue, 15 Oct 2024 13:02:18 +0900 Subject: [PATCH] .. --- app/src/main/AndroidManifest.xml | 2 + .../rasel/lunar/launcher/LauncherActivity.kt | 12 ++ .../rasel/lunar/launcher/feeds/Feeds.kt | 1 + .../launcher/workers/LocationUpdateService.kt | 161 ++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 app/src/main/kotlin/rasel/lunar/launcher/workers/LocationUpdateService.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 376c460..750a104 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -152,6 +152,8 @@ + + diff --git a/app/src/main/kotlin/rasel/lunar/launcher/LauncherActivity.kt b/app/src/main/kotlin/rasel/lunar/launcher/LauncherActivity.kt index d60290b..36efa5b 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/LauncherActivity.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/LauncherActivity.kt @@ -93,6 +93,7 @@ import rasel.lunar.launcher.helpers.Constants.Companion.PREFS_SETTINGS import rasel.lunar.launcher.helpers.Constants.Companion.widgetHostId import rasel.lunar.launcher.helpers.PrefHelper import rasel.lunar.launcher.helpers.ViewPagerAdapter +import rasel.lunar.launcher.helpers.letTrue import rasel.lunar.launcher.home.LauncherHome import rasel.lunar.launcher.home.LauncherHome.Companion.lastedFinishedPageUrl import rasel.lunar.launcher.home.LauncherHome.Companion.listTags @@ -118,6 +119,7 @@ import rasel.lunar.launcher.workers.DotaxGetter.Companion.COMIC2_WORK_TAG import rasel.lunar.launcher.workers.FmKoreaGetter import rasel.lunar.launcher.workers.FmKoreaGetter.Companion.COMIC_WORK_TAG import rasel.lunar.launcher.workers.LocationGetter +import rasel.lunar.launcher.workers.LocationUpdateService import rasel.lunar.launcher.workers.NewsFeedsGetter import rasel.lunar.launcher.workers.NewsFeedsGetter.Companion.FEDDS_WORK_TAG import rasel.lunar.launcher.workers.OpenWeatherGetter @@ -415,6 +417,16 @@ internal class LauncherActivity : AppCompatActivity() { } } } + updateLocationService() + } + + fun updateLocationService() { + PrefHelper.isLocationOn().letTrue { + startService(Intent(this, LocationUpdateService::class.java)) + } + if (PrefHelper.isLocationOn() == false) { + stopService(Intent(this, LocationUpdateService::class.java)) + } } override fun onDestroy() { diff --git a/app/src/main/kotlin/rasel/lunar/launcher/feeds/Feeds.kt b/app/src/main/kotlin/rasel/lunar/launcher/feeds/Feeds.kt index 9ce75b3..32edcbd 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/feeds/Feeds.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/feeds/Feeds.kt @@ -310,6 +310,7 @@ internal class Feeds : Fragment() , CommadCallabck { "loc_on" -> { PrefHelper.location(!PrefHelper.isLocationOn()) consoleLog("PrefHelper.isLocationOn() >>> ${PrefHelper.isLocationOn()}") + lActivity?.updateLocationService() } "cal" ->{ getCal() diff --git a/app/src/main/kotlin/rasel/lunar/launcher/workers/LocationUpdateService.kt b/app/src/main/kotlin/rasel/lunar/launcher/workers/LocationUpdateService.kt new file mode 100644 index 0000000..a69f428 --- /dev/null +++ b/app/src/main/kotlin/rasel/lunar/launcher/workers/LocationUpdateService.kt @@ -0,0 +1,161 @@ +package rasel.lunar.launcher.workers + +import android.Manifest +import android.app.Service +import android.content.Intent +import android.content.pm.PackageManager +import android.location.Geocoder +import android.location.Location +import android.location.LocationListener +import android.location.LocationManager +import android.os.Build +import android.os.IBinder +import android.widget.Toast +import androidx.core.app.ActivityCompat +import com.google.android.gms.location.LocationServices +import com.google.gson.Gson +import okhttp3.ConnectionPool +import okhttp3.MediaType +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.RequestBody +import okhttp3.Response +import okhttp3.ResponseBody +import rasel.lunar.launcher.helpers.PrefHelper +import rasel.lunar.launcher.helpers.letTrue +import rasel.lunar.launcher.model.LocationLog +import rasel.lunar.launcher.utils.BLog +import java.io.IOException +import java.math.BigDecimal +import java.math.RoundingMode +import java.util.Base64 +import java.util.Locale +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit + + +class LocationUpdateService : Service(), LocationListener { + + protected var locationManager: LocationManager? = null + var checkGPS = false + var checkNetwork = false + + // boolean canGetLocation = false; + var loc: Location? = null + + + override fun onBind(intent: Intent?): IBinder? { + TODO("Not yet implemented") + } + + override fun onLocationChanged(p0: Location) { + BLog.LOGE("p0") + PrefHelper.isLocationOn().letTrue { + pushLocation(p0.latitude,p0.longitude) + } + } + + override fun onCreate() { + super.onCreate() + location + } + + + private val location: Location? + private get() { + if (ActivityCompat.checkSelfPermission( + this, + Manifest.permission.ACCESS_FINE_LOCATION + ) != + PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( + this, + Manifest.permission.ACCESS_COARSE_LOCATION + ) + != PackageManager.PERMISSION_GRANTED + ) { + } + locationManager = applicationContext + .getSystemService(LOCATION_SERVICE) as LocationManager + checkGPS = locationManager!! + .isProviderEnabled(LocationManager.GPS_PROVIDER) + checkNetwork = locationManager!! + .isProviderEnabled(LocationManager.NETWORK_PROVIDER) + locationManager?.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this) + if (locationManager != null) { + val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this) + fusedLocationClient.lastLocation.addOnSuccessListener { location -> + if (location != null) { + Toast.makeText( + applicationContext, + java.lang.Double.toString(location.latitude) + location.longitude + "from method", + Toast.LENGTH_LONG + ).show() + pushLocation(location.latitude, location.longitude) + } + } + } +// Toast.makeText(getApplicationContext(), Double.toString(latitude) + longitude + "from method", Toast.LENGTH_LONG).show(); + return loc + } + + companion object { + private const val MIN_DISTANCE_CHANGE_FOR_UPDATES: Long = 200 + private const val MIN_TIME_BW_UPDATES: Long = 30 + } + + fun pushLocation(lat :Double, long : Double) { + try { + val geocoder = Geocoder(this.applicationContext, Locale.getDefault()) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + geocoder.getFromLocation(lat, long, 1) { addresses -> + addresses.first()?.let { + LocationLog().apply { + fillData(it) + Executors.newSingleThreadScheduledExecutor().schedule({ + try { + //////-1002450229641 + val url = + "https://lunaticbum.kr/bums/save/loc.api" + //7068729507 + // OkHttp 클라이언트 객체 생성 + val client = OkHttpClient.Builder() + .connectionPool(ConnectionPool(5, 60, TimeUnit.SECONDS)) + .build() + + // GET 요청 객체 생성 + val builder: Request.Builder = Request.Builder().url(url) + .addHeader("Content-Type", "application/json").get() + builder.method("POST", RequestBody.create( + MediaType.parse("application/text"), Base64.getEncoder().encode( + Gson().toJson(this@apply).toByteArray()))) + val request: Request = builder.build() + + BLog.LOGE("telegram before request ") + // OkHttp 클라이언트로 GET 요청 객체 전송 + val response: Response = client.newCall(request).execute() + if (response.isSuccessful()) { + // 응답 받아서 처리 + val body: ResponseBody? = response.body() + if (body != null) { + + } + } else BLog.LOGE("telegram Error Occurred") + + } catch (e: java.lang.Exception) { + e.printStackTrace() + } + }, 5, TimeUnit.SECONDS) + WorkersDb.getRealm().writeBlocking { + copyToRealm(this@apply) + } + } + } + addresses.forEach { } + } + } + } catch (e: IOException) { + e.printStackTrace() + } + } + +} \ No newline at end of file