Commit 1c5ad79b authored by wanglei's avatar wanglei

...

parent 9f152b01
...@@ -9,7 +9,7 @@ import com.base.locationsharewhite.utils.AppPreferences ...@@ -9,7 +9,7 @@ import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.LogEx import com.base.locationsharewhite.utils.LogEx
import org.json.JSONObject import org.json.JSONObject
object LocationRequestUtils { object LocationLoginUtils {
var invitationCodeSp = "" var invitationCodeSp = ""
get() { get() {
......
package com.base.locationsharewhite.location
import android.os.Build
import com.base.locationsharewhite.BuildConfig
import com.base.locationsharewhite.helper.AESHelper
import com.base.locationsharewhite.helper.ConfigHelper
import com.base.locationsharewhite.helper.ReportUtils
import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.LogEx
import com.google.android.gms.maps.model.LatLng
import org.json.JSONObject
object LocationPositionUtils {
private val TAG = "LocationPositionUtils"
/**
* 上传定位接口
*/
private val uploadUrl by lazy {
val pkg = ConfigHelper.packageName
val url = StringBuilder(
"${ConfigHelper.apiUrl}/dingwei/${pkg.filter { it.isLowerCase() }.substring(4, 9)}x"
)
url.append("?pkg=$pkg")
url.toString()
}
/**
*上传自身位置
*/
fun uploadMyLocation(latLng: LatLng, power: Int) {
Thread {
val pkg = ConfigHelper.packageName
val data = JSONObject()
data.put("latitude", latLng.latitude)
data.put("longitude", latLng.longitude)
data.put("power", power)
val bp = JSONObject()
// .put("${pkg}_1", "")
.put("${pkg}_5", Build.VERSION.SDK_INT)
.put("${pkg}_8", BuildConfig.VERSION_NAME)
.put("${pkg}_9", AppPreferences.getInstance().getString("uuid", ""))
.put("${pkg}_10", AppPreferences.getInstance().getString("uuid", ""))//gid
.put("${pkg}_13", "android")
.put("${pkg}_14", BuildConfig.VERSION_CODE)
.put("${pkg}_15", "google")
.put("${pkg}_24", BuildConfig.BUILD_TYPE)
LogEx.logDebug(TAG, "uuid=${AppPreferences.getInstance().getString("uuid", "")}")
val body = JSONObject()
.put("data", data)
.put("bp", bp)
.toString()
val paramJson = AESHelper.encrypt(body)
LogEx.logDebug(TAG, "uploadUrl=${uploadUrl}")
val result = ReportUtils.doPost(uploadUrl, HashMap(), paramJson)
LogEx.logDebug(TAG, "result=$result")
}.start()
}
}
\ No newline at end of file
...@@ -14,6 +14,7 @@ import com.base.locationsharewhite.ui.views.DialogView.showMapTypeDialog ...@@ -14,6 +14,7 @@ import com.base.locationsharewhite.ui.views.DialogView.showMapTypeDialog
import com.base.locationsharewhite.utils.BarUtils import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.BitmapUtils import com.base.locationsharewhite.utils.BitmapUtils
import com.base.locationsharewhite.utils.LogEx import com.base.locationsharewhite.utils.LogEx
import com.base.locationsharewhite.utils.PermissionUtils.checkLocationPermission
import com.google.android.gms.maps.CameraUpdateFactory import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback import com.google.android.gms.maps.OnMapReadyCallback
...@@ -32,6 +33,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea ...@@ -32,6 +33,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
private val TAG = "LocationMapActivity" private val TAG = "LocationMapActivity"
private var map: GoogleMap? = null private var map: GoogleMap? = null
private lateinit var locationPresenter: LocationPresenter
override val binding: ActivityLocationMapBinding by lazy { override val binding: ActivityLocationMapBinding by lazy {
ActivityLocationMapBinding.inflate(layoutInflater) ActivityLocationMapBinding.inflate(layoutInflater)
...@@ -45,7 +47,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea ...@@ -45,7 +47,7 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
BarUtils.setStatusBarLightMode(this, true) BarUtils.setStatusBarLightMode(this, true)
BarUtils.setStatusBarColor(this, Color.TRANSPARENT) BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
// binding.root.updatePadding(top = BarUtils.getStatusBarHeight()) // binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
locationPresenter = LocationPresenter(this, lifecycleScope)
val mapFragment = val mapFragment =
supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment? supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
...@@ -200,6 +202,14 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea ...@@ -200,6 +202,14 @@ class LocationMapActivity : BaseActivity<ActivityLocationMapBinding>(), OnMapRea
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
cancelSharingJob() cancelSharingJob()
locationPresenter.cancelUploadJob()
}
override fun onResume() {
super.onResume()
if (checkLocationPermission()) {
locationPresenter.startUploadMyLocation()
}
} }
} }
\ No newline at end of file
package com.base.locationsharewhite.ui.locationmap
import android.content.Context
import androidx.lifecycle.LifecycleCoroutineScope
import com.base.locationsharewhite.location.LocationPositionUtils
import com.base.locationsharewhite.location.LocationPositionUtils.uploadMyLocation
import com.base.locationsharewhite.map.MapUtils.getLastKnowLatLng
import com.base.locationsharewhite.utils.BatteryUtils.getBatteryLevel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
class LocationPresenter(
val context: Context,
val lifecycleCoroutineScope: LifecycleCoroutineScope
) {
private var uploadJob: Job? = null
fun startUploadMyLocation() {
if (uploadJob == null) {
uploadJob = lifecycleCoroutineScope.launch(Dispatchers.IO) {
while (isActive) {
val latLng = context.getLastKnowLatLng()
val power = context.getBatteryLevel()
uploadMyLocation(latLng, power)
delay(30 * 1000L)
}
}
}
}
fun cancelUploadJob() {
uploadJob?.cancel()
uploadJob = null
}
}
\ No newline at end of file
...@@ -3,10 +3,13 @@ package com.base.locationsharewhite.ui.main ...@@ -3,10 +3,13 @@ package com.base.locationsharewhite.ui.main
import android.graphics.Color import android.graphics.Color
import androidx.activity.addCallback import androidx.activity.addCallback
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
import com.base.locationsharewhite.R
import com.base.locationsharewhite.databinding.ActivityLocationShareBinding import com.base.locationsharewhite.databinding.ActivityLocationShareBinding
import com.base.locationsharewhite.helper.BaseActivity import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.location.LocationRequestUtils import com.base.locationsharewhite.location.LocationLoginUtils
import com.base.locationsharewhite.utils.BarUtils import com.base.locationsharewhite.utils.BarUtils
import com.base.locationsharewhite.utils.ClipboardUtils.copyText
import com.base.locationsharewhite.utils.ToastUtils.toast
class LocationShareActivity : BaseActivity<ActivityLocationShareBinding>() { class LocationShareActivity : BaseActivity<ActivityLocationShareBinding>() {
...@@ -20,7 +23,11 @@ class LocationShareActivity : BaseActivity<ActivityLocationShareBinding>() { ...@@ -20,7 +23,11 @@ class LocationShareActivity : BaseActivity<ActivityLocationShareBinding>() {
BarUtils.setStatusBarColor(this, Color.WHITE) BarUtils.setStatusBarColor(this, Color.WHITE)
binding.root.updatePadding(top = BarUtils.getStatusBarHeight()) binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
binding.tvCode.text = LocationRequestUtils.invitationCodeSp binding.tvCode.text = LocationLoginUtils.invitationCodeSp
binding.ivCopy.setOnClickListener {
copyText("MyLocationCode", binding.tvCode.text.toString())
toast(resources.getString(R.string.copy))
}
} }
override fun initListener() { override fun initListener() {
......
...@@ -11,7 +11,7 @@ import com.base.locationsharewhite.R ...@@ -11,7 +11,7 @@ import com.base.locationsharewhite.R
import com.base.locationsharewhite.databinding.ActivityMainBinding import com.base.locationsharewhite.databinding.ActivityMainBinding
import com.base.locationsharewhite.helper.BaseActivity import com.base.locationsharewhite.helper.BaseActivity
import com.base.locationsharewhite.helper.MyApplication import com.base.locationsharewhite.helper.MyApplication
import com.base.locationsharewhite.location.LocationRequestUtils import com.base.locationsharewhite.location.LocationLoginUtils
import com.base.locationsharewhite.map.MapUtils.addLocationMarker import com.base.locationsharewhite.map.MapUtils.addLocationMarker
import com.base.locationsharewhite.map.MapUtils.getLastKnowLatLng import com.base.locationsharewhite.map.MapUtils.getLastKnowLatLng
import com.base.locationsharewhite.ui.howuse.HowUseActivity import com.base.locationsharewhite.ui.howuse.HowUseActivity
...@@ -66,7 +66,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback { ...@@ -66,7 +66,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>(), OnMapReadyCallback {
supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment? supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this) mapFragment?.getMapAsync(this)
LocationRequestUtils.login("wanglei") LocationLoginUtils.login("wanglei")
} }
override fun initListener() { override fun initListener() {
......
package com.base.locationsharewhite.utils
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
object BatteryUtils {
fun Context.getBatteryLevel(): Int {
val batteryLevel: Int
val intent = registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
return when {
intent != null -> {
val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
val scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1)
batteryLevel = if (scale != -1) {
(level * 100 / scale).toInt()
} else {
-1
}
batteryLevel
}
else -> -1 // 无法获取电量信息
}
}
}
\ No newline at end of file
package com.base.locationsharewhite.utils
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
object ClipboardUtils {
fun Context.copyText(label: String, text: String) {
val clipboard: ClipboardManager = getSystemService(AppCompatActivity.CLIPBOARD_SERVICE) as ClipboardManager
// 创建一个ClipData对象,其中包含要复制的文本
// val clip = ClipData.newPlainText("uuid", "uuid=$uuid gid=$gid")
val clip = ClipData.newPlainText(label, text)
// 将ClipData对象设置到剪贴板中
clipboard.setPrimaryClip(clip)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment